Tim Waugh 1b8d44
--- grep-2.5.1/src/search.c	2004-12-31 15:28:35.720391036 +0000
Tim Waugh 1b8d44
+++ grep-2.5.1a/src/search.c	2005-01-07 14:53:10.308860193 +0000
Tim Waugh ff7a53
@@ -18,9 +18,13 @@
Tim Waugh ff7a53
 
Tim Waugh ff7a53
 /* Written August 1992 by Mike Haertel. */
Tim Waugh ff7a53
 
Tim Waugh ff7a53
+#ifndef _GNU_SOURCE
Tim Waugh ff7a53
+# define _GNU_SOURCE 1
Tim Waugh ff7a53
+#endif
Tim Waugh e7e163
 #ifdef HAVE_CONFIG_H
Tim Waugh e7e163
 # include <config.h>
Tim Waugh e7e163
 #endif
Tim Waugh e7e163
+#include <assert.h>
Tim Waugh e7e163
 #include <sys/types.h>
Tim Waugh e7e163
 #if defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H && defined HAVE_MBRTOWC
Tim Waugh e7e163
 /* We can handle multibyte string.  */
Tim Waugh ff7a53
@@ -39,6 +43,9 @@
Tim Waugh c3fd6f
 #ifdef HAVE_LIBPCRE
Tim Waugh c3fd6f
 # include <pcre.h>
Tim Waugh c3fd6f
 #endif
Tim Waugh c3fd6f
+#ifdef HAVE_LANGINFO_CODESET
Tim Waugh c3fd6f
+# include <langinfo.h>
Tim Waugh c3fd6f
+#endif
Tim Waugh c3fd6f
 
Tim Waugh c3fd6f
 #define NCHAR (UCHAR_MAX + 1)
Tim Waugh c3fd6f
 
Tim Waugh ff7a53
@@ -70,9 +77,10 @@
Tim Waugh e1ca94
    call the regexp matcher at all. */
Tim Waugh e1ca94
 static int kwset_exact_matches;
Tim Waugh e1ca94
 
Tim Waugh e1ca94
-#if defined(MBS_SUPPORT)
Tim Waugh e1ca94
-static char* check_multibyte_string PARAMS ((char const *buf, size_t size));
Tim Waugh e1ca94
-#endif
Tim Waugh c3fd6f
+/* UTF-8 encoding allows some optimizations that we can't otherwise
Tim Waugh c3fd6f
+   assume in a multibyte encoding. */
Tim Waugh c3fd6f
+static int using_utf8;
Tim Waugh c3fd6f
+
Tim Waugh e1ca94
 static void kwsinit PARAMS ((void));
Tim Waugh e1ca94
 static void kwsmusts PARAMS ((void));
Tim Waugh e1ca94
 static void Gcompile PARAMS ((char const *, size_t));
Tim Waugh ff7a53
@@ -84,6 +92,15 @@
Tim Waugh c3fd6f
 static size_t Pexecute PARAMS ((char const *, size_t, size_t *, int));
Tim Waugh c3fd6f
 
Tim Waugh c3fd6f
 void
Tim Waugh c3fd6f
+check_utf8 (void)
Tim Waugh c3fd6f
+{
Tim Waugh c3fd6f
+#ifdef HAVE_LANGINFO_CODESET
Tim Waugh c3fd6f
+  if (strcmp (nl_langinfo (CODESET), "UTF-8") == 0)
Tim Waugh c3fd6f
+    using_utf8 = 1;
Tim Waugh c3fd6f
+#endif
Tim Waugh c3fd6f
+}
Tim Waugh c3fd6f
+
Tim Waugh c3fd6f
+void
Tim Waugh c3fd6f
 dfaerror (char const *mesg)
Tim Waugh c3fd6f
 {
Tim Waugh c3fd6f
   error (2, 0, mesg);
Tim Waugh ff7a53
@@ -141,47 +158,6 @@
Tim Waugh e1ca94
     }
Tim Waugh e1ca94
 }
Tim Waugh e1ca94
 
Tim Waugh e1ca94
-#ifdef MBS_SUPPORT
Tim Waugh e1ca94
-/* This function allocate the array which correspond to "buf".
Tim Waugh e1ca94
-   Then this check multibyte string and mark on the positions which
Tim Waugh e1ca94
-   are not singlebyte character nor the first byte of a multibyte
Tim Waugh e1ca94
-   character.  Caller must free the array.  */
Tim Waugh e1ca94
-static char*
Tim Waugh e1ca94
-check_multibyte_string(char const *buf, size_t size)
Tim Waugh e1ca94
-{
Tim Waugh e1ca94
-  char *mb_properties = xmalloc(size);
Tim Waugh e1ca94
-  mbstate_t cur_state;
Tim Waugh e1ca94
-  wchar_t wc;
Tim Waugh e1ca94
-  int i;
Tim Waugh e1ca94
-  memset(&cur_state, 0, sizeof(mbstate_t));
Tim Waugh e1ca94
-  memset(mb_properties, 0, sizeof(char)*size);
Tim Waugh e1ca94
-  for (i = 0; i < size ;)
Tim Waugh e1ca94
-    {
Tim Waugh e1ca94
-      size_t mbclen;
Tim Waugh e1ca94
-      mbclen = mbrtowc(&wc, buf + i, size - i, &cur_state);
Tim Waugh e1ca94
-
Tim Waugh e1ca94
-      if (mbclen == (size_t) -1 || mbclen == (size_t) -2 || mbclen == 0)
Tim Waugh e1ca94
-	{
Tim Waugh e1ca94
-	  /* An invalid sequence, or a truncated multibyte character.
Tim Waugh e1ca94
-	     We treat it as a singlebyte character.  */
Tim Waugh e1ca94
-	  mbclen = 1;
Tim Waugh e1ca94
-	}
Tim Waugh e1ca94
-      else if (match_icase)
Tim Waugh e1ca94
-	{
Tim Waugh e1ca94
-	  if (iswupper((wint_t)wc))
Tim Waugh e1ca94
-	    {
Tim Waugh e1ca94
-	      wc = towlower((wint_t)wc);
Tim Waugh e1ca94
-	      wcrtomb(buf + i, wc, &cur_state);
Tim Waugh e1ca94
-	    }
Tim Waugh e1ca94
-	}
Tim Waugh e1ca94
-      mb_properties[i] = mbclen;
Tim Waugh e1ca94
-      i += mbclen;
Tim Waugh e1ca94
-    }
Tim Waugh e1ca94
-
Tim Waugh e1ca94
-  return mb_properties;
Tim Waugh e1ca94
-}
Tim Waugh e1ca94
-#endif
Tim Waugh e1ca94
-
Tim Waugh e1ca94
 static void
Tim Waugh e1ca94
 Gcompile (char const *pattern, size_t size)
Tim Waugh e1ca94
 {
Tim Waugh ff7a53
@@ -190,6 +166,7 @@
Tim Waugh c3fd6f
   size_t total = size;
Tim Waugh c3fd6f
   char const *motif = pattern;
Tim Waugh c3fd6f
 
Tim Waugh c3fd6f
+  check_utf8 ();
Tim Waugh c3fd6f
   re_set_syntax (RE_SYNTAX_GREP | RE_HAT_LISTS_NOT_NEWLINE | (match_icase ? RE_ICASE : 0));
Tim Waugh c3fd6f
   dfasyntax (RE_SYNTAX_GREP | RE_HAT_LISTS_NOT_NEWLINE, match_icase, eolbyte);
Tim Waugh c3fd6f
 
Tim Waugh ff7a53
@@ -266,6 +243,7 @@
Tim Waugh c3fd6f
   size_t total = size;
Tim Waugh c3fd6f
   char const *motif = pattern;
Tim Waugh c3fd6f
 
Tim Waugh c3fd6f
+  check_utf8 ();
Tim Waugh c3fd6f
   if (strcmp (matcher, "awk") == 0)
Tim Waugh c3fd6f
     {
Tim Waugh c3fd6f
       re_set_syntax (RE_SYNTAX_AWK | (match_icase ? RE_ICASE : 0));
Tim Waugh 1b8d44
@@ -350,18 +328,9 @@
Tim Waugh e1ca94
   struct kwsmatch kwsm;
Tim Waugh e1ca94
   size_t i, ret_val;
Tim Waugh e1ca94
 #ifdef MBS_SUPPORT
Tim Waugh e1ca94
-  char *mb_properties = NULL;
Tim Waugh e1ca94
-  if (MB_CUR_MAX > 1)
Tim Waugh e1ca94
-    {
Tim Waugh e1ca94
-      if (match_icase)
Tim Waugh e1ca94
-        {
Tim Waugh e1ca94
-          char *case_buf = xmalloc(size);
Tim Waugh e1ca94
-          memcpy(case_buf, buf, size);
Tim Waugh e1ca94
-          buf = case_buf;
Tim Waugh e1ca94
-        }
Tim Waugh e1ca94
-      if (kwset)
Tim Waugh e1ca94
-        mb_properties = check_multibyte_string(buf, size);
Tim Waugh e1ca94
-    }
Tim Waugh 1b8d44
+  int mb_cur_max = MB_CUR_MAX;
Tim Waugh e1ca94
+  mbstate_t mbs;
Tim Waugh e1ca94
+  memset (&mbs, '\0', sizeof (mbstate_t));
Tim Waugh e1ca94
 #endif /* MBS_SUPPORT */
Tim Waugh e1ca94
 
Tim Waugh e1ca94
   buflim = buf + size;
Tim Waugh 1b8d44
@@ -373,21 +342,63 @@
Tim Waugh 9fa0ec
 	  if (kwset)
Tim Waugh 9fa0ec
 	    {
Tim Waugh 9fa0ec
 	      /* Find a possible match using the KWset matcher. */
Tim Waugh 8443e1
-	      size_t offset = kwsexec (kwset, beg, buflim - beg, &kwsm);
Tim Waugh 9fa0ec
+#ifdef MBS_SUPPORT
Tim Waugh 9fa0ec
+	      size_t bytes_left = 0;
Tim Waugh 9fa0ec
+#endif /* MBS_SUPPORT */
Tim Waugh 8443e1
+	      size_t offset;
Tim Waugh 8443e1
+#ifdef MBS_SUPPORT
Tim Waugh a5ca95
+	      /* kwsexec doesn't work with match_icase and multibyte input. */
Tim Waugh 1b8d44
+	      if (match_icase && mb_cur_max > 1)
Tim Waugh 8443e1
+		/* Avoid kwset */
Tim Waugh 8443e1
+		offset = 0;
Tim Waugh 8443e1
+	      else
Tim Waugh 8443e1
+#endif /* MBS_SUPPORT */
Tim Waugh 8443e1
+	      offset = kwsexec (kwset, beg, buflim - beg, &kwsm);
Tim Waugh e1ca94
 	      if (offset == (size_t) -1)
Tim Waugh e1ca94
 	        goto failure;
Tim Waugh e1ca94
+#ifdef MBS_SUPPORT
Tim Waugh 1b8d44
+	      if (mb_cur_max > 1 && !using_utf8)
Tim Waugh e1ca94
+		{
Tim Waugh 9fa0ec
+		  bytes_left = offset;
Tim Waugh 9fa0ec
+		  while (bytes_left)
Tim Waugh e1ca94
+		    {
Tim Waugh b077d6
+		      size_t mlen = mbrlen (beg, bytes_left, &mbs);
Tim Waugh b077d6
+		      if (mlen == (size_t) -1 || mlen == 0)
Tim Waugh 6f2e16
+			{
Tim Waugh 2bb286
+			  /* Incomplete character: treat as single-byte. */
Tim Waugh 6f2e16
+			  memset (&mbs, '\0', sizeof (mbstate_t));
Tim Waugh 2bb286
+			  beg++;
Tim Waugh 2bb286
+			  bytes_left--;
Tim Waugh 2bb286
+			  continue;
Tim Waugh 6f2e16
+			}
Tim Waugh e1ca94
+
Tim Waugh b077d6
+		      if (mlen == (size_t) -2)
Tim Waugh 2bb286
+			/* Offset points inside multibyte character:
Tim Waugh 2bb286
+			 * no good. */
Tim Waugh 2bb286
+			break;
Tim Waugh 2bb286
+
Tim Waugh b077d6
+		      beg += mlen;
Tim Waugh b077d6
+		      bytes_left -= mlen;
Tim Waugh e1ca94
+		    }
Tim Waugh e1ca94
+		}
Tim Waugh 6f2e16
+	      else
Tim Waugh 6f2e16
+#endif /* MBS_SUPPORT */
Tim Waugh e1ca94
 	      beg += offset;
Tim Waugh e1ca94
 	      /* Narrow down to the line containing the candidate, and
Tim Waugh e1ca94
 		 run it through DFA. */
Tim Waugh e1ca94
 	      end = memchr(beg, eol, buflim - beg);
Tim Waugh e1ca94
 	      end++;
Tim Waugh 9fa0ec
 #ifdef MBS_SUPPORT
Tim Waugh e1ca94
-	      if (MB_CUR_MAX > 1 && mb_properties[beg - buf] == 0)
Tim Waugh 1b8d44
+	      if (mb_cur_max > 1 && bytes_left)
Tim Waugh 9fa0ec
 		continue;
Tim Waugh e1ca94
-#endif
Tim Waugh 9fa0ec
+#endif /* MBS_SUPPORT */
Tim Waugh e1ca94
 	      while (beg > buf && beg[-1] != eol)
Tim Waugh e1ca94
 		--beg;
Tim Waugh a5ca95
-	      if (kwsm.index < kwset_exact_matches)
Tim Waugh e5092c
+	      if (
Tim Waugh e5092c
+#ifdef MBS_SUPPORT
Tim Waugh 1b8d44
+		  !(match_icase && mb_cur_max > 1) &&
Tim Waugh e5092c
+#endif /* MBS_SUPPORT */
Tim Waugh a5ca95
+		  (kwsm.index < kwset_exact_matches))
Tim Waugh a5ca95
 		goto success_in_beg_and_end;
Tim Waugh a5ca95
 	      if (dfaexec (&dfa, beg, end - beg, &backref) == (size_t) -1)
Tim Waugh a5ca95
 		continue;
Tim Waugh 1b8d44
@@ -395,13 +406,47 @@
Tim Waugh 9fa0ec
 	  else
Tim Waugh 9fa0ec
 	    {
Tim Waugh 9fa0ec
 	      /* No good fixed strings; start with DFA. */
Tim Waugh 9fa0ec
+#ifdef MBS_SUPPORT
Tim Waugh 9fa0ec
+	      size_t bytes_left = 0;
Tim Waugh 9fa0ec
+#endif /* MBS_SUPPORT */
Tim Waugh 9fa0ec
 	      size_t offset = dfaexec (&dfa, beg, buflim - beg, &backref);
Tim Waugh e1ca94
 	      if (offset == (size_t) -1)
Tim Waugh e1ca94
 		break;
Tim Waugh e1ca94
 	      /* Narrow down to the line we've found. */
Tim Waugh e1ca94
+#ifdef MBS_SUPPORT
Tim Waugh 1b8d44
+	      if (mb_cur_max > 1 && !using_utf8)
Tim Waugh e1ca94
+		{
Tim Waugh 9fa0ec
+		  bytes_left = offset;
Tim Waugh 9fa0ec
+		  while (bytes_left)
Tim Waugh e1ca94
+		    {
Tim Waugh b077d6
+		      size_t mlen = mbrlen (beg, bytes_left, &mbs);
Tim Waugh b077d6
+		      if (mlen == (size_t) -1 || mlen == 0)
Tim Waugh 6f2e16
+			{
Tim Waugh 2bb286
+			  /* Incomplete character: treat as single-byte. */
Tim Waugh 6f2e16
+			  memset (&mbs, '\0', sizeof (mbstate_t));
Tim Waugh 2bb286
+			  beg++;
Tim Waugh 2bb286
+			  bytes_left--;
Tim Waugh 2bb286
+			  continue;
Tim Waugh 6f2e16
+			}
Tim Waugh e1ca94
+
Tim Waugh b077d6
+		      if (mlen == (size_t) -2)
Tim Waugh 2bb286
+			/* Offset points inside multibyte character:
Tim Waugh 2bb286
+			 * no good. */
Tim Waugh 2bb286
+			break;
Tim Waugh 2bb286
+
Tim Waugh b077d6
+		      beg += mlen;
Tim Waugh b077d6
+		      bytes_left -= mlen;
Tim Waugh e1ca94
+		    }
Tim Waugh e1ca94
+		}
Tim Waugh 6f2e16
+	      else
Tim Waugh 6f2e16
+#endif /* MBS_SUPPORT */
Tim Waugh e1ca94
 	      beg += offset;
Tim Waugh e1ca94
 	      end = memchr (beg, eol, buflim - beg);
Tim Waugh e1ca94
 	      end++;
Tim Waugh 9fa0ec
+#ifdef MBS_SUPPORT
Tim Waugh 1b8d44
+	      if (mb_cur_max > 1 && bytes_left)
Tim Waugh 9fa0ec
+		continue;
Tim Waugh 9fa0ec
+#endif /* MBS_SUPPORT */
Tim Waugh 9fa0ec
 	      while (beg > buf && beg[-1] != eol)
Tim Waugh 9fa0ec
 		--beg;
Tim Waugh 9fa0ec
 	    }
Tim Waugh 1b8d44
@@ -469,15 +514,6 @@
Tim Waugh e1ca94
     } /* for (beg = end ..) */
Tim Waugh e1ca94
 
Tim Waugh e1ca94
  failure:
Tim Waugh e1ca94
-#ifdef MBS_SUPPORT
Tim Waugh e1ca94
-  if (MB_CUR_MAX > 1)
Tim Waugh e1ca94
-    {
Tim Waugh e1ca94
-      if (mb_properties)
Tim Waugh e1ca94
-	free (mb_properties);
Tim Waugh e1ca94
-      if (match_icase)
Tim Waugh e1ca94
-	free ((char *) buf);
Tim Waugh e1ca94
-    }
Tim Waugh e1ca94
-#endif /* MBS_SUPPORT */
Tim Waugh e1ca94
   return (size_t) -1;
Tim Waugh e1ca94
 
Tim Waugh e1ca94
  success_in_beg_and_end:
Tim Waugh 1b8d44
@@ -486,24 +522,144 @@
Tim Waugh e1ca94
   /* FALLTHROUGH */
Tim Waugh e1ca94
 
Tim Waugh e1ca94
  success_in_start_and_len:
Tim Waugh e1ca94
-#ifdef MBS_SUPPORT
Tim Waugh e1ca94
-  if (MB_CUR_MAX > 1)
Tim Waugh e1ca94
-    {
Tim Waugh e1ca94
-      if (mb_properties)
Tim Waugh e1ca94
-	free (mb_properties);
Tim Waugh e1ca94
-      if (match_icase)
Tim Waugh e1ca94
-	free ((char *) buf);
Tim Waugh e1ca94
-    }
Tim Waugh e1ca94
-#endif /* MBS_SUPPORT */
Tim Waugh e1ca94
   *match_size = len;
Tim Waugh e1ca94
   return start;
Tim Waugh e1ca94
 }
Tim Waugh e7e163
 
Tim Waugh ff7a53
+#ifdef MBS_SUPPORT
Tim Waugh e7e163
+static int f_i_multibyte; /* whether we're using the new -Fi MB method */
Tim Waugh ff7a53
+static struct
Tim Waugh ff7a53
+{
Tim Waugh ff7a53
+  wchar_t **patterns;
Tim Waugh ff7a53
+  size_t count, maxlen;
Tim Waugh ff7a53
+  unsigned char *match;
Tim Waugh ff7a53
+} Fimb;
Tim Waugh ff7a53
+#endif
Tim Waugh e7e163
+
Tim Waugh e7e163
 static void
Tim Waugh e7e163
 Fcompile (char const *pattern, size_t size)
Tim Waugh c3fd6f
 {
Tim Waugh 1b8d44
+  int mb_cur_max = MB_CUR_MAX;
Tim Waugh c3fd6f
   char const *beg, *lim, *err;
Tim Waugh c3fd6f
 
Tim Waugh c3fd6f
+  check_utf8 ();
Tim Waugh e7e163
+#ifdef MBS_SUPPORT
Tim Waugh e7e163
+  /* Support -F -i for UTF-8 input. */
Tim Waugh 1b8d44
+  if (match_icase && mb_cur_max > 1)
Tim Waugh e7e163
+    {
Tim Waugh ff7a53
+      mbstate_t mbs;
Tim Waugh ff7a53
+      wchar_t *wcpattern = xmalloc ((size + 1) * sizeof (wchar_t));
Tim Waugh ff7a53
+      const char *patternend = pattern;
Tim Waugh ff7a53
+      size_t wcsize;
Tim Waugh ff7a53
+      kwset_t fimb_kwset = NULL;
Tim Waugh ff7a53
+      char *starts = NULL;
Tim Waugh ff7a53
+      wchar_t *wcbeg, *wclim;
Tim Waugh ff7a53
+      size_t allocated = 0;
Tim Waugh ff7a53
+
Tim Waugh ff7a53
+      memset (&mbs, '\0', sizeof (mbs));
Tim Waugh ff7a53
+# ifdef __GNU_LIBRARY__
Tim Waugh ff7a53
+      wcsize = mbsnrtowcs (wcpattern, &patternend, size, size, &mbs);
Tim Waugh ff7a53
+      if (patternend != pattern + size)
Tim Waugh ff7a53
+	wcsize = (size_t) -1;
Tim Waugh ff7a53
+# else
Tim Waugh ff7a53
+      {
Tim Waugh ff7a53
+	char *patterncopy = xmalloc (size + 1);
Tim Waugh ff7a53
+
Tim Waugh ff7a53
+	memcpy (patterncopy, pattern, size);
Tim Waugh ff7a53
+	patterncopy[size] = '\0';
Tim Waugh ff7a53
+	patternend = patterncopy;
Tim Waugh ff7a53
+	wcsize = mbsrtowcs (wcpattern, &patternend, size, &mbs);
Tim Waugh ff7a53
+	if (patternend != patterncopy + size)
Tim Waugh ff7a53
+	  wcsize = (size_t) -1;
Tim Waugh ff7a53
+	free (patterncopy);
Tim Waugh ff7a53
+      }
Tim Waugh ff7a53
+# endif
Tim Waugh ff7a53
+      if (wcsize + 2 <= 2)
Tim Waugh e7e163
+	{
Tim Waugh ff7a53
+fimb_fail:
Tim Waugh ff7a53
+	  free (wcpattern);
Tim Waugh ff7a53
+	  free (starts);
Tim Waugh ff7a53
+	  if (fimb_kwset)
Tim Waugh ff7a53
+	    kwsfree (fimb_kwset);
Tim Waugh ff7a53
+	  free (Fimb.patterns);
Tim Waugh ff7a53
+	  Fimb.patterns = NULL;
Tim Waugh ff7a53
+	}
Tim Waugh ff7a53
+      else
Tim Waugh ff7a53
+	{
Tim Waugh ff7a53
+	  if (!(fimb_kwset = kwsalloc (NULL)))
Tim Waugh ff7a53
+	    error (2, 0, _("memory exhausted"));
Tim Waugh ff7a53
+
Tim Waugh 1b8d44
+	  starts = xmalloc (mb_cur_max * 3);
Tim Waugh ff7a53
+	  wcbeg = wcpattern;
Tim Waugh ff7a53
+	  do
Tim Waugh e7e163
+	    {
Tim Waugh ff7a53
+	      int i;
Tim Waugh ff7a53
+	      size_t wclen;
Tim Waugh e7e163
+
Tim Waugh ff7a53
+	      if (Fimb.count >= allocated)
Tim Waugh e7e163
+		{
Tim Waugh ff7a53
+		  if (allocated == 0)
Tim Waugh ff7a53
+		    allocated = 128;
Tim Waugh ff7a53
+		  else
Tim Waugh ff7a53
+		    allocated *= 2;
Tim Waugh ff7a53
+		  Fimb.patterns = xrealloc (Fimb.patterns,
Tim Waugh ff7a53
+					    sizeof (wchar_t *) * allocated);
Tim Waugh e7e163
+		}
Tim Waugh ff7a53
+	      Fimb.patterns[Fimb.count++] = wcbeg;
Tim Waugh ff7a53
+	      for (wclim = wcbeg;
Tim Waugh ff7a53
+		   wclim < wcpattern + wcsize && *wclim != L'\n'; ++wclim)
Tim Waugh ff7a53
+		*wclim = towlower (*wclim);
Tim Waugh ff7a53
+	      *wclim = L'\0';
Tim Waugh ff7a53
+	      wclen = wclim - wcbeg;
Tim Waugh ff7a53
+	      if (wclen > Fimb.maxlen)
Tim Waugh ff7a53
+		Fimb.maxlen = wclen;
Tim Waugh ff7a53
+	      if (wclen > 3)
Tim Waugh ff7a53
+		wclen = 3;
Tim Waugh ff7a53
+	      if (wclen == 0)
Tim Waugh e7e163
+		{
Tim Waugh ff7a53
+		  if ((err = kwsincr (fimb_kwset, "", 0)) != 0)
Tim Waugh ff7a53
+		    error (2, 0, err);
Tim Waugh e7e163
+		}
Tim Waugh ff7a53
+	      else
Tim Waugh ff7a53
+		for (i = 0; i < (1 << wclen); i++)
Tim Waugh ff7a53
+		  {
Tim Waugh ff7a53
+		    char *p = starts;
Tim Waugh ff7a53
+		    int j, k;
Tim Waugh ff7a53
+
Tim Waugh ff7a53
+		    for (j = 0; j < wclen; ++j)
Tim Waugh ff7a53
+		      {
Tim Waugh ff7a53
+			wchar_t wc = wcbeg[j];
Tim Waugh ff7a53
+			if (i & (1 << j))
Tim Waugh ff7a53
+			  {
Tim Waugh ff7a53
+			    wc = towupper (wc);
Tim Waugh ff7a53
+			    if (wc == wcbeg[j])
Tim Waugh ff7a53
+			      continue;
Tim Waugh ff7a53
+			  }
Tim Waugh ff7a53
+			k = wctomb (p, wc);
Tim Waugh ff7a53
+			if (k <= 0)
Tim Waugh ff7a53
+			  goto fimb_fail;
Tim Waugh ff7a53
+			p += k;
Tim Waugh ff7a53
+		      }
Tim Waugh ff7a53
+		    if ((err = kwsincr (fimb_kwset, starts, p - starts)) != 0)
Tim Waugh ff7a53
+		      error (2, 0, err);
Tim Waugh ff7a53
+		  }
Tim Waugh ff7a53
+	      if (wclim < wcpattern + wcsize)
Tim Waugh ff7a53
+		++wclim;
Tim Waugh ff7a53
+	      wcbeg = wclim;
Tim Waugh e7e163
+	    }
Tim Waugh ff7a53
+	  while (wcbeg < wcpattern + wcsize);
Tim Waugh e7e163
+	  f_i_multibyte = 1;
Tim Waugh ff7a53
+	  kwset = fimb_kwset;
Tim Waugh ff7a53
+	  free (starts);
Tim Waugh ff7a53
+	  Fimb.match = xmalloc (Fimb.count);
Tim Waugh ff7a53
+	  if ((err = kwsprep (kwset)) != 0)
Tim Waugh ff7a53
+	    error (2, 0, err);
Tim Waugh ff7a53
+	  return;
Tim Waugh e7e163
+	}
Tim Waugh e7e163
+    }
Tim Waugh e7e163
+#endif /* MBS_SUPPORT */
Tim Waugh e7e163
+
Tim Waugh e7e163
+
Tim Waugh c3fd6f
   kwsinit ();
Tim Waugh c3fd6f
   beg = pattern;
Tim Waugh c3fd6f
   do
Tim Waugh 1b8d44
@@ -522,6 +678,76 @@
Tim Waugh ff7a53
     error (2, 0, err);
Tim Waugh e7e163
 }
Tim Waugh e7e163
 
Tim Waugh ff7a53
+#ifdef MBS_SUPPORT
Tim Waugh ff7a53
+static int
Tim Waugh ff7a53
+Fimbexec (const char *buf, size_t size, size_t *plen, int exact)
Tim Waugh e7e163
+{
Tim Waugh ff7a53
+  size_t len, letter, i;
Tim Waugh ff7a53
+  int ret = -1;
Tim Waugh e7e163
+  mbstate_t mbs;
Tim Waugh ff7a53
+  wchar_t wc;
Tim Waugh ff7a53
+  int patterns_left;
Tim Waugh e7e163
+
Tim Waugh e7e163
+  assert (match_icase && f_i_multibyte == 1);
Tim Waugh e7e163
+  assert (MB_CUR_MAX > 1);
Tim Waugh e7e163
+
Tim Waugh e7e163
+  memset (&mbs, '\0', sizeof (mbs));
Tim Waugh ff7a53
+  memset (Fimb.match, '\1', Fimb.count);
Tim Waugh ff7a53
+  letter = len = 0;
Tim Waugh ff7a53
+  patterns_left = 1;
Tim Waugh ff7a53
+  while (patterns_left && len <= size)
Tim Waugh e7e163
+    {
Tim Waugh ff7a53
+      size_t c;
Tim Waugh e7e163
+
Tim Waugh ff7a53
+      patterns_left = 0;
Tim Waugh ff7a53
+      if (len < size)
Tim Waugh ff7a53
+	{
Tim Waugh ff7a53
+	  c = mbrtowc (&wc, buf + len, size - len, &mbs);
Tim Waugh ff7a53
+	  if (c + 2 <= 2)
Tim Waugh ff7a53
+	    return ret;
Tim Waugh e7e163
+
Tim Waugh e7e163
+	  wc = towlower (wc);
Tim Waugh ff7a53
+	}
Tim Waugh ff7a53
+      else
Tim Waugh ff7a53
+	{
Tim Waugh ff7a53
+	  c = 1;
Tim Waugh ff7a53
+	  wc = L'\0';
Tim Waugh ff7a53
+	}
Tim Waugh ff7a53
+
Tim Waugh ff7a53
+      for (i = 0; i < Fimb.count; i++)
Tim Waugh ff7a53
+	{
Tim Waugh ff7a53
+	  if (Fimb.match[i])
Tim Waugh e7e163
+	    {
Tim Waugh ff7a53
+	      if (Fimb.patterns[i][letter] == L'\0')
Tim Waugh e7e163
+		{
Tim Waugh ff7a53
+		  /* Found a match. */
Tim Waugh ff7a53
+		  *plen = len;
Tim Waugh ff7a53
+		  if (!exact && !match_words)
Tim Waugh ff7a53
+		    return 0;
Tim Waugh ff7a53
+		  else
Tim Waugh e7e163
+		    {
Tim Waugh ff7a53
+		      /* For -w or exact look for longest match.  */
Tim Waugh ff7a53
+		      ret = 0;
Tim Waugh ff7a53
+		      Fimb.match[i] = '\0';
Tim Waugh ff7a53
+		      continue;
Tim Waugh e7e163
+		    }
Tim Waugh e7e163
+		}
Tim Waugh e7e163
+
Tim Waugh ff7a53
+	      if (Fimb.patterns[i][letter] == wc)
Tim Waugh ff7a53
+		patterns_left = 1;
Tim Waugh ff7a53
+	      else
Tim Waugh ff7a53
+		Fimb.match[i] = '\0';
Tim Waugh ff7a53
+	    }
Tim Waugh e7e163
+	}
Tim Waugh e7e163
+
Tim Waugh ff7a53
+      len += c;
Tim Waugh ff7a53
+      letter++;
Tim Waugh e7e163
+    }
Tim Waugh ff7a53
+
Tim Waugh ff7a53
+  return ret;
Tim Waugh e7e163
+}
Tim Waugh ff7a53
+#endif /* MBS_SUPPORT */
Tim Waugh e7e163
+
Tim Waugh ff7a53
 static size_t
Tim Waugh e7e163
 Fexecute (char const *buf, size_t size, size_t *match_size, int exact)
Tim Waugh e7e163
 {
Tim Waugh 1b8d44
@@ -531,80 +757,258 @@
Tim Waugh e1ca94
   struct kwsmatch kwsmatch;
Tim Waugh e1ca94
   size_t ret_val;
Tim Waugh e1ca94
 #ifdef MBS_SUPPORT
Tim Waugh e1ca94
-  char *mb_properties = NULL;
Tim Waugh e1ca94
-  if (MB_CUR_MAX > 1)
Tim Waugh e1ca94
-    {
Tim Waugh e1ca94
-      if (match_icase)
Tim Waugh e1ca94
-        {
Tim Waugh e1ca94
-          char *case_buf = xmalloc(size);
Tim Waugh e1ca94
-          memcpy(case_buf, buf, size);
Tim Waugh e1ca94
-          buf = case_buf;
Tim Waugh e1ca94
-        }
Tim Waugh e1ca94
-      mb_properties = check_multibyte_string(buf, size);
Tim Waugh e1ca94
-    }
Tim Waugh ff7a53
+  int mb_cur_max = MB_CUR_MAX;
Tim Waugh e1ca94
+  mbstate_t mbs;
Tim Waugh e1ca94
+  memset (&mbs, '\0', sizeof (mbstate_t));
Tim Waugh ff7a53
+  const char *last_char = NULL;
Tim Waugh e1ca94
 #endif /* MBS_SUPPORT */
Tim Waugh e1ca94
 
Tim Waugh e1ca94
   for (beg = buf; beg <= buf + size; ++beg)
Tim Waugh e7e163
     {
Tim Waugh e7e163
-      size_t offset = kwsexec (kwset, beg, buf + size - beg, &kwsmatch);
Tim Waugh e7e163
+      size_t offset;
Tim Waugh e7e163
+      offset = kwsexec (kwset, beg, buf + size - beg, &kwsmatch);
Tim Waugh e7e163
+
Tim Waugh e1ca94
       if (offset == (size_t) -1)
Tim Waugh e1ca94
 	goto failure;
Tim Waugh e1ca94
 #ifdef MBS_SUPPORT
Tim Waugh e1ca94
-      if (MB_CUR_MAX > 1 && mb_properties[offset+beg-buf] == 0)
Tim Waugh e1ca94
-	continue; /* It is a part of multibyte character.  */
Tim Waugh ff7a53
+      if (mb_cur_max > 1 && !using_utf8)
Tim Waugh e1ca94
+	{
Tim Waugh 9fa0ec
+	  size_t bytes_left = offset;
Tim Waugh 9fa0ec
+	  while (bytes_left)
Tim Waugh e1ca94
+	    {
Tim Waugh ff7a53
+	      size_t mlen = mbrlen (beg, bytes_left, &mbs);
Tim Waugh ff7a53
+
Tim Waugh ff7a53
+	      last_char = beg;
Tim Waugh ff7a53
+	      if (mlen == (size_t) -1 || mlen == 0)
Tim Waugh 6f2e16
+		{
Tim Waugh 2bb286
+		  /* Incomplete character: treat as single-byte. */
Tim Waugh 6f2e16
+		  memset (&mbs, '\0', sizeof (mbstate_t));
Tim Waugh 2bb286
+		  beg++;
Tim Waugh 2bb286
+		  bytes_left--;
Tim Waugh 2bb286
+		  continue;
Tim Waugh 6f2e16
+		}
Tim Waugh 6f2e16
+
Tim Waugh ff7a53
+	      if (mlen == (size_t) -2)
Tim Waugh 2bb286
+		/* Offset points inside multibyte character: no good. */
Tim Waugh 2bb286
+		break;
Tim Waugh 2bb286
+
Tim Waugh ff7a53
+	      beg += mlen;
Tim Waugh ff7a53
+	      bytes_left -= mlen;
Tim Waugh e1ca94
+	    }
Tim Waugh e1ca94
+
Tim Waugh 9fa0ec
+	  if (bytes_left)
Tim Waugh 6f2e16
+	    continue;
Tim Waugh e1ca94
+	}
Tim Waugh 6f2e16
+      else
Tim Waugh 6f2e16
 #endif /* MBS_SUPPORT */
Tim Waugh e1ca94
       beg += offset;
Tim Waugh e7e163
+#ifdef MBS_SUPPORT
Tim Waugh ff7a53
+      /* For f_i_multibyte, the string at beg now matches first 3 chars of
Tim Waugh ff7a53
+	 one of the search strings (less if there are shorter search strings).
Tim Waugh ff7a53
+	 See if this is a real match.  */
Tim Waugh ff7a53
+      if (f_i_multibyte
Tim Waugh ff7a53
+	  && Fimbexec (beg, buf + size - beg, &kwsmatch.size[0], exact))
Tim Waugh ff7a53
+	goto next_char;
Tim Waugh ff7a53
+#endif /* MBS_SUPPORT */
Tim Waugh ff7a53
       len = kwsmatch.size[0];
Tim Waugh ff7a53
       if (exact && !match_words)
Tim Waugh ff7a53
 	goto success_in_beg_and_len;
Tim Waugh ff7a53
       if (match_lines)
Tim Waugh ff7a53
 	{
Tim Waugh ff7a53
 	  if (beg > buf && beg[-1] != eol)
Tim Waugh ff7a53
-	    continue;
Tim Waugh ff7a53
+	    goto next_char;
Tim Waugh ff7a53
 	  if (beg + len < buf + size && beg[len] != eol)
Tim Waugh ff7a53
-	    continue;
Tim Waugh ff7a53
+	    goto next_char;
Tim Waugh ff7a53
 	  goto success;
Tim Waugh ff7a53
 	}
Tim Waugh ff7a53
       else if (match_words)
Tim Waugh ff7a53
-	for (try = beg; len; )
Tim Waugh ff7a53
-	  {
Tim Waugh ff7a53
-	    if (try > buf && WCHAR((unsigned char) try[-1]))
Tim Waugh ff7a53
-	      break;
Tim Waugh ff7a53
-	    if (try + len < buf + size && WCHAR((unsigned char) try[len]))
Tim Waugh ff7a53
-	      {
Tim Waugh ff7a53
-		offset = kwsexec (kwset, beg, --len, &kwsmatch);
Tim Waugh ff7a53
-		if (offset == (size_t) -1)
Tim Waugh ff7a53
-		  {
Tim Waugh ff7a53
+	{
Tim Waugh ff7a53
+	  while (1)
Tim Waugh ff7a53
+	    {
Tim Waugh ff7a53
+	      int word_match = 0;
Tim Waugh ff7a53
+	      if (beg > buf)
Tim Waugh ff7a53
+		{
Tim Waugh ff7a53
 #ifdef MBS_SUPPORT
Tim Waugh ff7a53
-		    if (MB_CUR_MAX > 1)
Tim Waugh ff7a53
-		      free (mb_properties);
Tim Waugh ff7a53
+		  if (mb_cur_max > 1)
Tim Waugh ff7a53
+		    {
Tim Waugh ff7a53
+		      const char *s;
Tim Waugh ff7a53
+		      int mr;
Tim Waugh ff7a53
+		      wchar_t pwc;
Tim Waugh ff7a53
+
Tim Waugh ff7a53
+		      if (using_utf8)
Tim Waugh ff7a53
+			{
Tim Waugh ff7a53
+			  s = beg - 1;
Tim Waugh ff7a53
+			  while (s > buf
Tim Waugh ff7a53
+				 && (unsigned char) *s >= 0x80
Tim Waugh ff7a53
+				 && (unsigned char) *s <= 0xbf)
Tim Waugh ff7a53
+			    --s;
Tim Waugh ff7a53
+			}
Tim Waugh ff7a53
+		      else
Tim Waugh ff7a53
+			s = last_char;
Tim Waugh ff7a53
+		      mr = mbtowc (&pwc, s, beg - s);
Tim Waugh ff7a53
+		      if (mr <= 0)
Tim Waugh ff7a53
+			memset (&mbs, '\0', sizeof (mbstate_t));
Tim Waugh ff7a53
+		      else if ((iswalnum (pwc) || pwc == L'_')
Tim Waugh ff7a53
+			       && mr == (int) (beg - s))
Tim Waugh ff7a53
+			goto next_char;
Tim Waugh ff7a53
+		    }
Tim Waugh e7e163
+		  else
Tim Waugh ff7a53
 #endif /* MBS_SUPPORT */
Tim Waugh ff7a53
-		    return offset;
Tim Waugh ff7a53
-		  }
Tim Waugh ff7a53
-		try = beg + offset;
Tim Waugh ff7a53
-		len = kwsmatch.size[0];
Tim Waugh ff7a53
-	      }
Tim Waugh ff7a53
-	    else
Tim Waugh ff7a53
-	      goto success;
Tim Waugh ff7a53
-	  }
Tim Waugh b566e4
+		  if (WCHAR ((unsigned char) beg[-1]))
Tim Waugh ff7a53
+		    goto next_char;
Tim Waugh ff7a53
+		}
Tim Waugh ff7a53
+#ifdef MBS_SUPPORT
Tim Waugh ff7a53
+	      if (mb_cur_max > 1)
Tim Waugh ff7a53
+		{
Tim Waugh ff7a53
+		  wchar_t nwc;
Tim Waugh ff7a53
+		  int mr;
Tim Waugh ff7a53
+
Tim Waugh ff7a53
+		  mr = mbtowc (&nwc, beg + len, buf + size - beg - len);
Tim Waugh ff7a53
+		  if (mr <= 0)
Tim Waugh ff7a53
+		    {
Tim Waugh ff7a53
+		      memset (&mbs, '\0', sizeof (mbstate_t));
Tim Waugh ff7a53
+		      word_match = 1;
Tim Waugh ff7a53
+		    }
Tim Waugh ff7a53
+		  else if (!iswalnum (nwc) && nwc != L'_')
Tim Waugh ff7a53
+		    word_match = 1;
Tim Waugh ff7a53
+		}
Tim Waugh ff7a53
+	      else
Tim Waugh e7e163
+#endif /* MBS_SUPPORT */
Tim Waugh 23c991
+		if (beg + len >= buf + size || !WCHAR ((unsigned char) beg[len]))
Tim Waugh ff7a53
+		  word_match = 1;
Tim Waugh ff7a53
+	      if (word_match)
Tim Waugh ff7a53
+		{
Tim Waugh ff7a53
+		  if (!exact)
Tim Waugh ff7a53
+		    /* Returns the whole line now we know there's a word match. */
Tim Waugh ff7a53
+		    goto success;
Tim Waugh ff7a53
+		  else
Tim Waugh ff7a53
+		    /* Returns just this word match. */
Tim Waugh ff7a53
+		    goto success_in_beg_and_len;
Tim Waugh ff7a53
+		}
Tim Waugh ff7a53
+	      if (len > 0)
Tim Waugh ff7a53
+		{
Tim Waugh ff7a53
+		  /* Try a shorter length anchored at the same place. */
Tim Waugh ff7a53
+		  --len;
Tim Waugh ff7a53
+		  offset = kwsexec (kwset, beg, len, &kwsmatch);
Tim Waugh e7e163
+
Tim Waugh ff7a53
+		  if (offset == -1)
Tim Waugh ff7a53
+		    goto next_char; /* Try a different anchor. */
Tim Waugh e1ca94
+#ifdef MBS_SUPPORT
Tim Waugh ff7a53
+		  if (mb_cur_max > 1 && !using_utf8)
Tim Waugh e1ca94
+		    {
Tim Waugh 9fa0ec
+		      size_t bytes_left = offset;
Tim Waugh 9fa0ec
+		      while (bytes_left)
Tim Waugh e1ca94
+			{
Tim Waugh ff7a53
+			  size_t mlen = mbrlen (beg, bytes_left, &mbs);
Tim Waugh ff7a53
+
Tim Waugh ff7a53
+			  last_char = beg;
Tim Waugh ff7a53
+			  if (mlen == (size_t) -1 || mlen == 0)
Tim Waugh 6f2e16
+			    {
Tim Waugh 2bb286
+			      /* Incomplete character: treat as single-byte. */
Tim Waugh 6f2e16
+			      memset (&mbs, '\0', sizeof (mbstate_t));
Tim Waugh 2bb286
+			      beg++;
Tim Waugh 2bb286
+			      bytes_left--;
Tim Waugh 2bb286
+			      continue;
Tim Waugh 6f2e16
+			    }
Tim Waugh 6f2e16
+
Tim Waugh ff7a53
+			  if (mlen == (size_t) -2)
Tim Waugh ff7a53
+			    {
Tim Waugh ff7a53
+			      /* Offset points inside multibyte character:
Tim Waugh ff7a53
+			       * no good. */
Tim Waugh ff7a53
+			      break;
Tim Waugh ff7a53
+			    }
Tim Waugh 2bb286
+
Tim Waugh ff7a53
+			  beg += mlen;
Tim Waugh ff7a53
+			  bytes_left -= mlen;
Tim Waugh e1ca94
+			}
Tim Waugh e1ca94
+
Tim Waugh 9fa0ec
+		      if (bytes_left)
Tim Waugh ff7a53
+			{
Tim Waugh ff7a53
+			  memset (&mbs, '\0', sizeof (mbstate_t));
Tim Waugh ff7a53
+			  goto next_char; /* Try a different anchor. */
Tim Waugh ff7a53
+			}
Tim Waugh e1ca94
+		    }
Tim Waugh 6f2e16
+		  else
Tim Waugh e1ca94
+#endif /* MBS_SUPPORT */
Tim Waugh ff7a53
+		  beg += offset;
Tim Waugh ff7a53
+#ifdef MBS_SUPPORT
Tim Waugh ff7a53
+		  /* The string at beg now matches first 3 chars of one of
Tim Waugh ff7a53
+		     the search strings (less if there are shorter search
Tim Waugh ff7a53
+		     strings).  See if this is a real match.  */
Tim Waugh ff7a53
+		  if (f_i_multibyte
Tim Waugh ff7a53
+		      && Fimbexec (beg, len - offset, &kwsmatch.size[0],
Tim Waugh ff7a53
+				   exact))
Tim Waugh ff7a53
+		    goto next_char;
Tim Waugh ff7a53
+#endif /* MBS_SUPPORT */
Tim Waugh ff7a53
+		  len = kwsmatch.size[0];
Tim Waugh ff7a53
+		}
Tim Waugh ff7a53
+	    }
Tim Waugh ff7a53
+	}
Tim Waugh ff7a53
       else
Tim Waugh ff7a53
 	goto success;
Tim Waugh ff7a53
+next_char:;
Tim Waugh ff7a53
+#ifdef MBS_SUPPORT
Tim Waugh ff7a53
+      /* Advance to next character.  For MB_CUR_MAX == 1 case this is handled
Tim Waugh ff7a53
+	 by ++beg above.  */
Tim Waugh ff7a53
+      if (mb_cur_max > 1)
Tim Waugh ff7a53
+	{
Tim Waugh ff7a53
+	  if (using_utf8)
Tim Waugh ff7a53
+	    {
Tim Waugh ff7a53
+	      unsigned char c = *beg;
Tim Waugh ff7a53
+	      if (c >= 0xc2)
Tim Waugh ff7a53
+		{
Tim Waugh ff7a53
+		  if (c < 0xe0)
Tim Waugh ff7a53
+		    ++beg;
Tim Waugh ff7a53
+		  else if (c < 0xf0)
Tim Waugh ff7a53
+		    beg += 2;
Tim Waugh ff7a53
+		  else if (c < 0xf8)
Tim Waugh ff7a53
+		    beg += 3;
Tim Waugh ff7a53
+		  else if (c < 0xfc)
Tim Waugh ff7a53
+		    beg += 4;
Tim Waugh ff7a53
+		  else if (c < 0xfe)
Tim Waugh ff7a53
+		    beg += 5;
Tim Waugh ff7a53
+		}
Tim Waugh ff7a53
+	    }
Tim Waugh ff7a53
+	  else
Tim Waugh ff7a53
+	    {
Tim Waugh ff7a53
+	      size_t l = mbrlen (beg, buf + size - beg, &mbs);
Tim Waugh ff7a53
+
Tim Waugh ff7a53
+	      last_char = beg;
Tim Waugh ff7a53
+	      if (l + 2 >= 2)
Tim Waugh ff7a53
+		beg += l - 1;
Tim Waugh ff7a53
+	      else
Tim Waugh ff7a53
+		memset (&mbs, '\0', sizeof (mbstate_t));
Tim Waugh ff7a53
+	    }
Tim Waugh ff7a53
+	}
Tim Waugh ff7a53
+#endif /* MBS_SUPPORT */
Tim Waugh e1ca94
     }
Tim Waugh e1ca94
 
Tim Waugh e1ca94
  failure:
Tim Waugh c3fd6f
+  return -1;
Tim Waugh c3fd6f
+
Tim Waugh c3fd6f
+ success:
Tim Waugh c3fd6f
 #ifdef MBS_SUPPORT
Tim Waugh e1ca94
-  if (MB_CUR_MAX > 1)
Tim Waugh ff7a53
+  if (mb_cur_max > 1 && !using_utf8)
Tim Waugh c3fd6f
     {
Tim Waugh e1ca94
-      if (match_icase)
Tim Waugh e1ca94
-        free((char *) buf);
Tim Waugh e1ca94
-      if (mb_properties)
Tim Waugh e1ca94
-        free(mb_properties);
Tim Waugh c3fd6f
+      end = beg + len;
Tim Waugh c3fd6f
+      while (end < buf + size)
Tim Waugh e1ca94
+	{
Tim Waugh b077d6
+	  size_t mlen = mbrlen (end, buf + size - end, &mbs);
Tim Waugh b077d6
+	  if (mlen == (size_t) -1 || mlen == (size_t) -2 || mlen == 0)
Tim Waugh c3fd6f
+	    {
Tim Waugh c3fd6f
+	      memset (&mbs, '\0', sizeof (mbstate_t));
Tim Waugh b077d6
+	      mlen = 1;
Tim Waugh c3fd6f
+	    }
Tim Waugh b077d6
+	  if (mlen == 1 && *end == eol)
Tim Waugh c3fd6f
+	    break;
Tim Waugh e1ca94
+
Tim Waugh b077d6
+	  end += mlen;
Tim Waugh c3fd6f
+	}
Tim Waugh c3fd6f
     }
Tim Waugh c3fd6f
+  else
Tim Waugh c3fd6f
 #endif /* MBS_SUPPORT */
Tim Waugh c3fd6f
-  return -1;
Tim Waugh c3fd6f
-
Tim Waugh c3fd6f
- success:
Tim Waugh e1ca94
   end = memchr (beg + len, eol, (buf + size) - (beg + len));
Tim Waugh c3fd6f
+
Tim Waugh e1ca94
   end++;
Tim Waugh e1ca94
   while (buf < beg && beg[-1] != eol)
Tim Waugh e1ca94
     --beg;
Tim Waugh 1b8d44
@@ -613,15 +1017,6 @@
Tim Waugh e1ca94
 
Tim Waugh e1ca94
  success_in_beg_and_len:
Tim Waugh e1ca94
   *match_size = len;
Tim Waugh e1ca94
-#ifdef MBS_SUPPORT
Tim Waugh e1ca94
-  if (MB_CUR_MAX > 1)
Tim Waugh e1ca94
-    {
Tim Waugh e1ca94
-      if (mb_properties)
Tim Waugh e1ca94
-	free (mb_properties);
Tim Waugh e1ca94
-      if (match_icase)
Tim Waugh e1ca94
-	free ((char *) buf);
Tim Waugh e1ca94
-    }
Tim Waugh e1ca94
-#endif /* MBS_SUPPORT */
Tim Waugh e1ca94
   return beg - buf;
Tim Waugh e1ca94
 }
Tim Waugh e1ca94