Mika Fischer <[EMAIL PROTECTED]> tried to post the attached grep patch for
mitigating the 100x slowdown in UTF-8 locales to this list, but his mail
never got through.

Please let him know what you think about it.

Markus

P.S.: Who should one contact if there is a malfunction of the linux-utf8
list server? Others have complained before that they can't post here.

-- 
Markus Kuhn, Computer Laboratory, University of Cambridge
http://www.cl.cam.ac.uk/~mgk25/ || CB3 0FD, Great Britain

--- search.c.orig       2003-11-10 12:18:23.000000000 +0100
+++ search.c    2003-11-10 13:10:46.000000000 +0100
@@ -17,6 +17,8 @@
    02111-1307, USA.  */
 
 /* Written August 1992 by Mike Haertel. */
+/* 2003-11-10 Mika Fischer <[EMAIL PROTECTED]>:
+ * Do not compute character lengths in advance, do it when needed. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
@@ -141,38 +143,6 @@
     }
 }
 
-#ifdef MBS_SUPPORT
-/* This function allocate the array which correspond to "buf".
-   Then this check multibyte string and mark on the positions which
-   are not singlebyte character nor the first byte of a multibyte
-   character.  Caller must free the array.  */
-static char*
-check_multibyte_string(char const *buf, size_t size)
-{
-  char *mb_properties = malloc(size);
-  mbstate_t cur_state;
-  int i;
-  memset(&cur_state, 0, sizeof(mbstate_t));
-  memset(mb_properties, 0, sizeof(char)*size);
-  for (i = 0; i < size ;)
-    {
-      size_t mbclen;
-      mbclen = mbrlen(buf + i, size - i, &cur_state);
-
-      if (mbclen == (size_t) -1 || mbclen == (size_t) -2 || mbclen == 0)
-       {
-         /* An invalid sequence, or a truncated multibyte character.
-            We treat it as a singlebyte character.  */
-         mbclen = 1;
-       }
-      mb_properties[i] = mbclen;
-      i += mbclen;
-    }
-
-  return mb_properties;
-}
-#endif
-
 static void
 Gcompile (char const *pattern, size_t size)
 {
@@ -341,12 +311,7 @@
   struct kwsmatch kwsm;
   size_t i;
 #ifdef MBS_SUPPORT
-  char *mb_properties = NULL;
-#endif /* MBS_SUPPORT */
-
-#ifdef MBS_SUPPORT
-  if (MB_CUR_MAX > 1 && kwset)
-    mb_properties = check_multibyte_string(buf, size);
+  mbstate_t cur_state;
 #endif /* MBS_SUPPORT */
 
   buflim = buf + size;
@@ -361,10 +326,6 @@
              size_t offset = kwsexec (kwset, beg, buflim - beg, &kwsm);
              if (offset == (size_t) -1)
                {
-#ifdef MBS_SUPPORT
-                 if (MB_CUR_MAX > 1)
-                   free(mb_properties);
-#endif
                  return (size_t)-1;
                }
              beg += offset;
@@ -373,8 +334,12 @@
              end = memchr(beg, eol, buflim - beg);
              end++;
 #ifdef MBS_SUPPORT
-             if (MB_CUR_MAX > 1 && mb_properties[beg - buf] == 0)
-               continue;
+             if (MB_CUR_MAX > 1)
+               {
+                 memset(&cur_state, 0, sizeof(mbstate_t));
+                 if (mbrlen(beg, buflim - beg, &cur_state) < 0)
+                   continue;
+               }
 #endif
              while (beg > buf && beg[-1] != eol)
                --beg;
@@ -461,17 +426,9 @@
            }
        } /* for Regex patterns.  */
     } /* for (beg = end ..) */
-#ifdef MBS_SUPPORT
-  if (MB_CUR_MAX > 1 && mb_properties)
-    free (mb_properties);
-#endif /* MBS_SUPPORT */
   return (size_t) -1;
 
  success:
-#ifdef MBS_SUPPORT
-  if (MB_CUR_MAX > 1 && mb_properties)
-    free (mb_properties);
-#endif /* MBS_SUPPORT */
   *match_size = end - beg;
   return beg - buf;
 }
@@ -507,9 +464,7 @@
   char eol = eolbyte;
   struct kwsmatch kwsmatch;
 #ifdef MBS_SUPPORT
-  char *mb_properties;
-  if (MB_CUR_MAX > 1)
-    mb_properties = check_multibyte_string (buf, size);
+  mbstate_t cur_state;
 #endif /* MBS_SUPPORT */
 
   for (beg = buf; beg <= buf + size; ++beg)
@@ -517,25 +472,21 @@
       size_t offset = kwsexec (kwset, beg, buf + size - beg, &kwsmatch);
       if (offset == (size_t) -1)
        {
-#ifdef MBS_SUPPORT
-         if (MB_CUR_MAX > 1)
-           free(mb_properties);
-#endif /* MBS_SUPPORT */
          return offset;
        }
 #ifdef MBS_SUPPORT
-      if (MB_CUR_MAX > 1 && mb_properties[offset+beg-buf] == 0)
-       continue; /* It is a part of multibyte character.  */
+      if (MB_CUR_MAX > 1)
+       {
+         memset(&cur_state, 0, sizeof(mbstate_t));
+         if (mbrlen(beg + offset, buf + size - beg, &cur_state) < 0)
+           continue; /* It is a part of multibyte character.  */
+       }
 #endif /* MBS_SUPPORT */
       beg += offset;
       len = kwsmatch.size[0];
       if (exact)
        {
          *match_size = len;
-#ifdef MBS_SUPPORT
-         if (MB_CUR_MAX > 1)
-           free (mb_properties);
-#endif /* MBS_SUPPORT */
          return beg - buf;
        }
       if (match_lines)
@@ -556,10 +507,6 @@
                offset = kwsexec (kwset, beg, --len, &kwsmatch);
                if (offset == (size_t) -1)
                  {
-#ifdef MBS_SUPPORT
-                   if (MB_CUR_MAX > 1)
-                     free (mb_properties);
-#endif /* MBS_SUPPORT */
                    return offset;
                  }
                try = beg + offset;
@@ -572,10 +519,6 @@
        goto success;
     }
 
-#ifdef MBS_SUPPORT
-  if (MB_CUR_MAX > 1)
-    free (mb_properties);
-#endif /* MBS_SUPPORT */
   return -1;
 
  success:
@@ -584,10 +527,6 @@
   while (buf < beg && beg[-1] != eol)
     --beg;
   *match_size = end - beg;
-#ifdef MBS_SUPPORT
-  if (MB_CUR_MAX > 1)
-    free (mb_properties);
-#endif /* MBS_SUPPORT */
   return beg - buf;
 }
 

Reply via email to