1 /* kwsearch.c - searching subroutines using kwset for grep.
2 Copyright 1992, 1998, 2000, 2007, 2009-2012 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
19 /* Written August 1992 by Mike Haertel. */
24 /* For -w, we also consider _ to be word constituent. */
25 #define WCHAR(C) (isalnum (C) || (C) == '_')
27 /* KWset compiled pattern. For Ecompile and Gcompile, we compile
28 a list of strings, at least one of which is known to occur in
29 any string matching the regexp. */
33 Fcompile (char const *pattern, size_t size)
37 char const *pat = (match_icase && MB_CUR_MAX > 1
38 ? mbtolower (pattern, &psize)
43 char const *beg = pat;
48 for (lim = beg;; ++lim)
51 if (lim >= pat + psize)
58 #if HAVE_DOS_FILE_CONTENTS
59 if (*lim == '\r' && lim + 1 < pat + psize && lim[1] == '\n')
67 if ((err = kwsincr (kwset, beg, end - beg)) != NULL)
68 error (EXIT_TROUBLE, 0, "%s", err);
71 while (beg < pat + psize);
73 if ((err = kwsprep (kwset)) != NULL)
74 error (EXIT_TROUBLE, 0, "%s", err);
78 Fexecute (char const *buf, size_t size, size_t *match_size,
79 char const *start_ptr)
81 char const *beg, *try, *end, *mb_start;
84 struct kwsmatch kwsmatch;
90 char *case_buf = mbtolower (buf, &size);
92 start_ptr = case_buf + (start_ptr - buf);
97 for (mb_start = beg = start_ptr ? start_ptr : buf; beg <= buf + size; beg++)
99 size_t offset = kwsexec (kwset, beg, buf + size - beg, &kwsmatch);
100 if (offset == (size_t) -1)
102 len = kwsmatch.size[0];
104 && is_mb_middle (&mb_start, beg + offset, buf + size, len))
106 /* The match was a part of multibyte character, advance at least
107 one byte to ensure no infinite loop happens. */
109 memset (&s, 0, sizeof s);
110 size_t mb_len = mbrlen (mb_start, (buf + size) - (beg + offset), &s);
111 if (mb_len == (size_t) -2)
114 if (mb_len != (size_t) -1)
119 if (start_ptr && !match_words)
120 goto success_in_beg_and_len;
123 if (beg > buf && beg[-1] != eol)
125 if (beg + len < buf + size && beg[len] != eol)
129 else if (match_words)
132 if (try > buf && WCHAR((unsigned char) try[-1]))
134 if (try + len < buf + size && WCHAR((unsigned char) try[len]))
138 offset = kwsexec (kwset, beg, --len, &kwsmatch);
139 if (offset == (size_t) -1)
142 len = kwsmatch.size[0];
147 goto success_in_beg_and_len;
151 } /* for (beg in buf) */
158 if ((end = memchr (beg + len, eol, (buf + size) - (beg + len))) != NULL)
162 while (buf < beg && beg[-1] != eol)
165 success_in_beg_and_len: