]> Savannah Git Hosting - gnulib.git/commitdiff
Optimize away some mbsinit calls.
authorBruno Haible <bruno@clisp.org>
Tue, 11 Jul 2023 19:30:44 +0000 (21:30 +0200)
committerBruno Haible <bruno@clisp.org>
Tue, 11 Jul 2023 19:30:44 +0000 (21:30 +0200)
* lib/mbiter.h (mbiter_multi_next): When the module 'mbrtoc32-regular'
is in use, don't invoke mbsinit and don't compare the mbrtoc32 result
against (size_t)(-3).
* lib/mbuiter.h (mbuiter_multi_next): Likewise.
* lib/mbfile.h (mbfile_multi_getc): Likewise.
* lib/mbswidth.c (mbsnwidth): Likewise.
* lib/mbmemcasecoll.c (apply_c32tolower): Likewise.
* lib/quotearg.c (quotearg_buffer_restyled): Likewise.

ChangeLog
lib/mbfile.h
lib/mbiter.h
lib/mbmemcasecoll.c
lib/mbswidth.c
lib/mbuiter.h
lib/quotearg.c

index 602e56f7d1814c611734a8bf1176f1847a6af61f..f233e3c5dbffd5dd686d9096b271cbce819192aa 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2023-07-11  Bruno Haible  <bruno@clisp.org>
+
+       Optimize away some mbsinit calls.
+       * lib/mbiter.h (mbiter_multi_next): When the module 'mbrtoc32-regular'
+       is in use, don't invoke mbsinit and don't compare the mbrtoc32 result
+       against (size_t)(-3).
+       * lib/mbuiter.h (mbuiter_multi_next): Likewise.
+       * lib/mbfile.h (mbfile_multi_getc): Likewise.
+       * lib/mbswidth.c (mbsnwidth): Likewise.
+       * lib/mbmemcasecoll.c (apply_c32tolower): Likewise.
+       * lib/quotearg.c (quotearg_buffer_restyled): Likewise.
+
 2023-07-11  Bruno Haible  <bruno@clisp.org>
 
        Fix build errors on Linux/hppa.
index 6c971e64abd02a568a71f2d6a3fa94f3704ad922..74d49865773557d9df25ee370b955478cb6e56e4 100644 (file)
@@ -97,7 +97,11 @@ mbfile_multi_getc (struct mbchar *mbc, struct mbfile_multi *mbf)
 
   /* If mbf->state is not in an initial state, some more 32-bit wide character
      may be hiding in the state.  We need to call mbrtoc32 again.  */
+  #if GNULIB_MBRTOC32_REGULAR
+  assert (mbsinit (&mbf->state));
+  #else
   if (mbsinit (&mbf->state))
+  #endif
     {
       /* Before using mbrtoc32, we need at least one byte.  */
       if (new_bufcount == 0)
@@ -185,10 +189,12 @@ mbfile_multi_getc (struct mbchar *mbc, struct mbfile_multi *mbf)
               assert (mbf->buf[0] == '\0');
               assert (mbc->wc == 0);
             }
+          #if !GNULIB_MBRTOC32_REGULAR
           else if (bytes == (size_t) -3)
             /* The previous multibyte sequence produced an additional 32-bit
                wide character.  */
             bytes = 0;
+          #endif
           mbc->wc_valid = true;
           break;
         }
index 963ccff1f7e0dd1986314110625dfd28161c9dbc..179338a856042cd4d94a974c1f68766172da1181 100644 (file)
@@ -169,15 +169,19 @@ mbiter_multi_next (struct mbiter_multi *iter)
               assert (*iter->cur.ptr == '\0');
               assert (iter->cur.wc == 0);
             }
+          #if !GNULIB_MBRTOC32_REGULAR
           else if (iter->cur.bytes == (size_t) -3)
             /* The previous multibyte sequence produced an additional 32-bit
                wide character.  */
             iter->cur.bytes = 0;
+          #endif
           iter->cur.wc_valid = true;
 
           /* When in an initial state, we can go back treating ASCII
              characters more quickly.  */
+          #if !GNULIB_MBRTOC32_REGULAR
           if (mbsinit (&iter->state))
+          #endif
             iter->in_shift = false;
         }
     }
index f79f262dc0c04e8c2c6cc0ac6327908ab8394cc0..0b765ff05f6efd48ce88ad7c3115581132c0e65b 100644 (file)
@@ -54,7 +54,7 @@ apply_c32tolower (const char *inbuf, size_t inbufsize,
       mbstate_t state;
 
       memset (&state, '\0', sizeof (mbstate_t));
-      do
+      for (;;)
         {
           char32_t wc1;
           size_t n1;
@@ -86,8 +86,10 @@ apply_c32tolower (const char *inbuf, size_t inbufsize,
 
               if (n1 == 0) /* NUL character? */
                 n1 = 1;
+              #if !GNULIB_MBRTOC32_REGULAR
               else if (n1 == (size_t)(-3))
                 n1 = 0;
+              #endif
 
               wc2 = c32tolower (wc1);
               if (wc2 != wc1)
@@ -112,8 +114,11 @@ apply_c32tolower (const char *inbuf, size_t inbufsize,
               inbuf += n1;
               remaining -= n1;
             }
+          #if !GNULIB_MBRTOC32_REGULAR
+          if (mbsinit (&state))
+          #endif
+            break;
         }
-      while (! mbsinit (&state));
     }
 
   /* Verify the output buffer was large enough.  */
index 6b26c6a59957931e1245e2431124b35303e5a33a..a1613dcad6fd3e15262766b789524ec6c1c469d5 100644 (file)
@@ -95,7 +95,7 @@ mbsnwidth (const char *string, size_t nbytes, int flags)
               {
                 mbstate_t mbstate;
                 memset (&mbstate, 0, sizeof mbstate);
-                do
+                for (;;)
                   {
                     char32_t wc;
                     size_t bytes;
@@ -132,8 +132,10 @@ mbsnwidth (const char *string, size_t nbytes, int flags)
                     if (bytes == 0)
                       /* A null wide character was encountered.  */
                       bytes = 1;
+                    #if !GNULIB_MBRTOC32_REGULAR
                     else if (bytes == (size_t) -3)
                       bytes = 0;
+                    #endif
 
                     w = c32width (wc);
                     if (w >= 0)
@@ -158,8 +160,11 @@ mbsnwidth (const char *string, size_t nbytes, int flags)
                         return -1;
 
                     p += bytes;
+                    #if !GNULIB_MBRTOC32_REGULAR
+                    if (mbsinit (&mbstate))
+                    #endif
+                      break;
                   }
-                while (! mbsinit (&mbstate));
               }
               break;
           }
index 7900a48715099cb067e96bf8fdc636224de54472..7cadb8b402ed4351a6518ad589636636b3cb9756 100644 (file)
@@ -176,15 +176,19 @@ mbuiter_multi_next (struct mbuiter_multi *iter)
               assert (*iter->cur.ptr == '\0');
               assert (iter->cur.wc == 0);
             }
+          #if !GNULIB_MBRTOC32_REGULAR
           else if (iter->cur.bytes == (size_t) -3)
             /* The previous multibyte sequence produced an additional 32-bit
                wide character.  */
             iter->cur.bytes = 0;
+          #endif
           iter->cur.wc_valid = true;
 
           /* When in an initial state, we can go back treating ASCII
              characters more quickly.  */
+          #if !GNULIB_MBRTOC32_REGULAR
           if (mbsinit (&iter->state))
+          #endif
             iter->in_shift = false;
         }
     }
index db915efa673e9abc2a548a2981cfd5595db5c855..5b26055b2e551abf566df0b95c10b3610dc0c14a 100644 (file)
@@ -614,7 +614,7 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize,
                 if (argsize == SIZE_MAX)
                   argsize = strlen (arg);
 
-                do
+                for (;;)
                   {
                     char32_t w;
                     size_t bytes = mbrtoc32 (&w, &arg[i + m],
@@ -635,8 +635,10 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize,
                       }
                     else
                       {
+                        #if !GNULIB_MBRTOC32_REGULAR
                         if (bytes == (size_t) -3)
                           bytes = 0;
+                        #endif
                         /* Work around a bug with older shells that "see" a '\'
                            that is really the 2nd byte of a multibyte character.
                            In practice the problem is limited to ASCII
@@ -661,8 +663,11 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize,
                           printable = false;
                         m += bytes;
                       }
+                    #if !GNULIB_MBRTOC32_REGULAR
+                    if (mbsinit (&mbstate))
+                    #endif
+                      break;
                   }
-                while (! mbsinit (&mbstate));
               }
 
             c_and_shell_quote_compat = printable;