bkabrda / rpms / grep

Forked from rpms/grep 6 years ago
Clone
a8485b3
--- grep-2.5.1a/src/search.c.w	2006-02-20 14:27:27.000000000 +0000
a8485b3
+++ grep-2.5.1a/src/search.c	2006-02-20 14:32:07.000000000 +0000
a8485b3
@@ -507,10 +507,114 @@
f416c42
 	      if (match_words)
f416c42
 		while (start >= 0)
f416c42
 		  {
f416c42
-		    if ((start == 0 || !WCHAR ((unsigned char) beg[start - 1]))
f416c42
-			&& (len == end - beg - 1
f416c42
-			    || !WCHAR ((unsigned char) beg[start + len])))
f416c42
-		      goto success_in_beg_and_end;
f416c42
+		    int lword_match = 0;
f416c42
+		    if (start == 0)
f416c42
+		      lword_match = 1;
f416c42
+		    else
f416c42
+		      {
f416c42
+			assert (start > 0);
f416c42
+#ifdef MBS_SUPPORT
f416c42
+			if (mb_cur_max > 1)
f416c42
+			  {
f416c42
+			    const char *s;
a8485b3
+			    size_t mr;
f416c42
+			    wchar_t pwc;
f416c42
+
a8485b3
+			    /* Locate the start of the multibyte character
a8485b3
+			       before the match position (== beg + start). */
f416c42
+			    if (using_utf8)
f416c42
+			      {
a8485b3
+				/* UTF-8 is a special case: scan backwards
a8485b3
+				   until we find a 7-bit character or a
a8485b3
+				   lead byte. */
f416c42
+				s = beg + start - 1;
f416c42
+				while (s > buf
f416c42
+				       && (unsigned char) *s >= 0x80
f416c42
+				       && (unsigned char) *s <= 0xbf)
f416c42
+				  --s;
f416c42
+			      }
f416c42
+			    else
a8485b3
+			      {
a8485b3
+				/* Scan forwards to find the start of the
a8485b3
+				   last complete character before the
a8485b3
+				   match position.  */
a8485b3
+				size_t bytes_left = start - 1;
a8485b3
+				s = beg;
a8485b3
+				while (bytes_left > 0)
a8485b3
+				  {
a8485b3
+				    mr = mbrlen (s, bytes_left, &mbs);
a8485b3
+				    if (mr == (size_t) -1 || mr == 0)
a8485b3
+				      {
a8485b3
+					memset (&mbs, '\0', sizeof (mbs));
a8485b3
+					s++;
a8485b3
+					bytes_left--;
a8485b3
+					continue;
a8485b3
+				      }
a8485b3
+				    if (mr == (size_t) -2)
a8485b3
+				      {
a8485b3
+					memset (&mbs, '\0', sizeof (mbs));
a8485b3
+					break;
a8485b3
+				      }
a8485b3
+				    s += mr;
a8485b3
+				    bytes_left -= mr;
a8485b3
+				  }
a8485b3
+			      }
a8485b3
+			    mr = mbrtowc (&pwc, s, beg + start - s, &mbs);
a8485b3
+			    if (mr == (size_t) -2 || mr == (size_t) -1 ||
a8485b3
+				mr == 0)
f416c42
+			      {
f416c42
+				memset (&mbs, '\0', sizeof (mbstate_t));
f416c42
+				lword_match = 1;
f416c42
+			      }
f416c42
+			    else if (!(iswalnum (pwc) || pwc == L'_')
a8485b3
+				     && mr == beg + start - s)
f416c42
+			      lword_match = 1;
f416c42
+			  }
f416c42
+			else
f416c42
+#endif /* MBS_SUPPORT */
f416c42
+			if (!WCHAR ((unsigned char) beg[start - 1]))
f416c42
+			  lword_match = 1;
f416c42
+		      }
f416c42
+
f416c42
+		    if (lword_match)
f416c42
+		      {
f416c42
+			int rword_match = 0;
f416c42
+			if (start + len == end - beg - 1)
f416c42
+			  rword_match = 1;
f416c42
+			else
f416c42
+			  {
f416c42
+#ifdef MBS_SUPPORT
f416c42
+			    if (mb_cur_max > 1)
f416c42
+			      {
f416c42
+				wchar_t nwc;
f416c42
+				int mr;
f416c42
+
f416c42
+				mr = mbtowc (&nwc, beg + start + len,
f416c42
+					     end - beg - start - len - 1);
f416c42
+				if (mr <= 0)
f416c42
+				  {
f416c42
+				    memset (&mbs, '\0', sizeof (mbstate_t));
f416c42
+				    rword_match = 1;
f416c42
+				  }
f416c42
+				else if (!iswalnum (nwc) && nwc != L'_')
f416c42
+				  rword_match = 1;
f416c42
+			      }
f416c42
+			    else
f416c42
+#endif /* MBS_SUPPORT */
f416c42
+			    if (!WCHAR ((unsigned char) beg[start + len]))
f416c42
+			      rword_match = 1;
f416c42
+			  }
f416c42
+
f416c42
+			if (rword_match)
f416c42
+			  {
f416c42
+			    if (!exact)
f416c42
+			      /* Returns the whole line. */
f416c42
+			      goto success_in_beg_and_end;
f416c42
+			    else
f416c42
+			      /* Returns just this word match. */
f416c42
+			      goto success_in_start_and_len;
f416c42
+			  }
f416c42
+		      }
f416c42
 		    if (len > 0)
f416c42
 		      {
f416c42
 			/* Try a shorter length anchored at the same place. */