PERFORCE change 144876 for review

Gabor Kovesdan gabor at FreeBSD.org
Tue Jul 8 12:15:54 UTC 2008


http://perforce.freebsd.org/chv.cgi?CH=144876

Change 144876 by gabor at gabor_server on 2008/07/08 12:15:14

	- Make -w respect wide characters

Affected files ...

.. //depot/projects/soc2008/gabor_textproc/grep/util.c#51 edit

Differences ...

==== //depot/projects/soc2008/gabor_textproc/grep/util.c#51 (text+ko) ====

@@ -45,6 +45,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <wchar.h>
+#include <wctype.h>
 
 #include "grep.h"
 
@@ -218,7 +220,7 @@
 	return (c);
 }
 
-#define isword(x)	(isalnum((unsigned char)(x)) || (x) == '_')
+#define iswword(x)	(iswalnum((x)) || (x) == L'_')
 
 static int
 procline(struct str *l, int nottext)
@@ -242,10 +244,37 @@
 				if (r == 0 && xflag)
 					if (pmatch.rm_so != 0 || pmatch.rm_eo != l->len)
 						r = REG_NOMATCH;
-				if (r == 0 && wflag)
-					if ((pmatch.rm_so != 0 && isword((unsigned char)l->dat[pmatch.rm_so - 1]))
-					    || (pmatch.rm_eo != l->len && isword((unsigned char)l->dat[pmatch.rm_eo])))
+				if (r == 0 && wflag) {
+					char	*begin, *end;
+					wchar_t	*wbegin, *wend;
+					size_t	size;
+
+					begin = grep_malloc(sizeof(char) * (pmatch.rm_so + 1));
+					end = grep_malloc(sizeof(char) * (strlen(l->dat) - pmatch.rm_eo));
+					strlcpy(begin, l->dat, pmatch.rm_so);
+					strlcpy(end, &(l->dat[pmatch.rm_eo]), (strlen(l->dat) - pmatch.rm_eo));
+
+					size = mbstowcs(NULL, begin, 0);
+					wbegin = grep_malloc(sizeof(wint_t) * size);
+					if (mbstowcs(wbegin, begin, size) == -1) {
+						r = REG_NOMATCH;
+						continue;
+					}
+					free(begin);
+					size = mbstowcs(NULL, end, 0);
+					wend = grep_malloc(sizeof(wint_t) * size);
+					if (mbstowcs(wend, end, size) == -1) {
+						r = REG_NOMATCH;
+						continue;
+					}
+					free(end);
+
+					if ((pmatch.rm_so != 0 && iswword(wbegin[wcslen(wbegin -1)]))
+					    || (pmatch.rm_eo != l->len && iswword(wend[0])))
 						r = REG_NOMATCH;
+					free(wbegin);
+					free(wend);
+				}
 				if (r == t) {
 					if (m == 0)
 						c++;


More information about the p4-projects mailing list