1 /* $OpenBSD: util.c,v 1.36 2007/10/02 17:59:18 otto Exp $ */
2 /* $NetBSD: fastgrep.c,v 1.4 2011/02/27 17:33:37 joerg Exp $ */
6 * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
7 * Copyright (C) 2008 Gabor Kovesdan <gabor@FreeBSD.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * XXX: This file is a speed up for grep to cover the defects of the
34 * regex library. These optimizations should practically be implemented
35 * there keeping this code clean. This is a future TODO, but for the
36 * meantime, we need to use this workaround.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
51 static inline int grep_cmp(const unsigned char *, const unsigned char *, size_t);
52 static inline void grep_revstr(unsigned char *, int);
55 fgrepcomp(fastgrep_t *fg, const char *pat)
60 fg->len = strlen(pat);
65 fg->pattern = (unsigned char *)grep_strdup(pat);
67 /* Preprocess pattern. */
68 for (i = 0; i <= UCHAR_MAX; i++)
69 fg->qsBc[i] = fg->len;
70 for (i = 1; i < fg->len; i++)
71 fg->qsBc[fg->pattern[i]] = fg->len - i;
75 * Returns: -1 on failure, 0 on success
78 fastcomp(fastgrep_t *fg, const char *pat)
81 int firstHalfDot = -1;
82 int firstLastHalfDot = -1;
88 fg->len = strlen(pat);
94 /* Remove end-of-line character ('$'). */
95 if (fg->len > 0 && pat[fg->len - 1] == '$') {
100 /* Remove beginning-of-line character ('^'). */
108 memcmp(pat, "[[:<:]]", 7) == 0 &&
109 memcmp(pat + fg->len - 7, "[[:>:]]", 7) == 0) {
112 /* Word boundary is handled separately in util.c */
117 * pat has been adjusted earlier to not include '^', '$' or
118 * the word match character classes at the beginning and ending
119 * of the string respectively.
121 fg->pattern = grep_malloc(fg->len + 1);
122 memcpy(fg->pattern, pat, fg->len);
123 fg->pattern[fg->len] = '\0';
125 /* Look for ways to cheat...er...avoid the full regex engine. */
126 for (i = 0; i < fg->len; i++) {
127 /* Can still cheat? */
128 if (fg->pattern[i] == '.') {
130 if (i < fg->len / 2) {
131 if (firstHalfDot < 0)
132 /* Closest dot to the beginning */
135 /* Closest dot to the end of the pattern. */
137 if (firstLastHalfDot < 0)
138 firstLastHalfDot = i;
141 /* Free memory and let others know this is empty. */
149 * Determine if a reverse search would be faster based on the placement
152 if ((!(lflag || cflag)) && ((!(fg->bol || fg->eol)) &&
153 ((lastHalfDot) && ((firstHalfDot < 0) ||
154 ((fg->len - (lastHalfDot + 1)) < (size_t)firstHalfDot)))) &&
157 hasDot = fg->len - (firstHalfDot < 0 ?
158 firstLastHalfDot : firstHalfDot) - 1;
159 grep_revstr(fg->pattern, fg->len);
163 * Normal Quick Search would require a shift based on the position the
164 * next character after the comparison is within the pattern. With
165 * wildcards, the position of the last dot effects the maximum shift
167 * The closer to the end the wild card is the slower the search. A
168 * reverse version of this algorithm would be useful for wildcards near
169 * the end of the string.
181 /* Adjust the shift based on location of the last dot ('.'). */
182 shiftPatternLen = fg->len - hasDot;
184 /* Preprocess pattern. */
185 for (i = 0; i <= (signed)UCHAR_MAX; i++)
186 fg->qsBc[i] = shiftPatternLen;
187 for (i = hasDot + 1; i < fg->len; i++) {
188 fg->qsBc[fg->pattern[i]] = fg->len - i;
192 * Put pattern back to normal after pre-processing to allow for easy
196 grep_revstr(fg->pattern, fg->len);
202 grep_search(fastgrep_t *fg, const unsigned char *data, size_t len, regmatch_t *pmatch)
205 int ret = REG_NOMATCH;
207 if (pmatch->rm_so == (ssize_t)len)
210 if (fg->bol && pmatch->rm_so != 0) {
216 /* No point in going farther if we do not have enough data. */
220 /* Only try once at the beginning or ending of the line. */
221 if (fg->bol || fg->eol) {
222 /* Simple text comparison. */
223 /* Verify data is >= pattern length before searching on it. */
224 if (len >= fg->len) {
225 /* Determine where in data to start search at. */
226 j = fg->eol ? len - fg->len : 0;
227 if (!((fg->bol && fg->eol) && (len != fg->len)))
228 if (grep_cmp(fg->pattern, data + j,
231 pmatch->rm_eo = j + fg->len;
235 } else if (fg->reversed) {
236 /* Quick Search algorithm. */
239 if (grep_cmp(fg->pattern, data + j - fg->len,
241 pmatch->rm_so = j - fg->len;
246 /* Shift if within bounds, otherwise, we are done. */
249 j -= fg->qsBc[data[j - fg->len - 1]];
250 } while (j >= fg->len);
252 /* Quick Search algorithm. */
255 if (grep_cmp(fg->pattern, data + j, fg->len) == -1) {
257 pmatch->rm_eo = j + fg->len;
262 /* Shift if within bounds, otherwise, we are done. */
263 if (j + fg->len == len)
266 j += fg->qsBc[data[j + fg->len]];
267 } while (j <= (len - fg->len));
274 * Returns: i >= 0 on failure (position that it failed)
278 grep_cmp(const unsigned char *pat, const unsigned char *data, size_t len)
281 wchar_t *wdata, *wpat;
285 if ((size = mbstowcs(NULL, (const char *)data, 0)) ==
289 wdata = grep_malloc(size * sizeof(wint_t));
291 if (mbstowcs(wdata, (const char *)data, size) ==
295 if ((size = mbstowcs(NULL, (const char *)pat, 0)) ==
299 wpat = grep_malloc(size * sizeof(wint_t));
301 if (mbstowcs(wpat, (const char *)pat, size) == ((size_t) - 1))
303 for (i = 0; i < len; i++) {
304 if ((towlower(wpat[i]) == towlower(wdata[i])) ||
305 ((grepbehave != GREP_FIXED) && wpat[i] == L'.'))
312 for (i = 0; i < len; i++) {
313 if ((pat[i] == data[i]) || ((grepbehave != GREP_FIXED) &&
323 grep_revstr(unsigned char *str, int len)
328 for (i = 0; i < len / 2; i++) {
330 str[i] = str[len - i - 1];
331 str[len - i - 1] = c;