PERFORCE change 147093 for review
Gabor Kovesdan
gabor at FreeBSD.org
Sun Aug 10 17:41:26 UTC 2008
http://perforce.freebsd.org/chv.cgi?CH=147093
Change 147093 by gabor at gabor_server on 2008/08/10 17:40:42
- More C-ish variable names
- style(9)
Affected files ...
.. //depot/projects/soc2008/gabor_textproc/grep/fastgrep.c#6 edit
.. //depot/projects/soc2008/gabor_textproc/grep/grep.h#41 edit
Differences ...
==== //depot/projects/soc2008/gabor_textproc/grep/fastgrep.c#6 (text+ko) ====
@@ -49,35 +49,35 @@
int i;
/* Initialize. */
- fg->patternLen = strlen(pattern);
+ fg->len = strlen(pattern);
fg->bol = 0;
fg->eol = 0;
- fg->reversedSearch = 0;
+ fg->reversed = 0;
/*
* Make a copy and upper case it for later if in -i mode,
* else just copy the pointer.
*/
if (iflag) {
- fg->pattern = grep_malloc(fg->patternLen + 1);
- for (i = 0; i < fg->patternLen; i++)
+ fg->pattern = grep_malloc(fg->len + 1);
+ for (i = 0; i < fg->len; i++)
fg->pattern[i] = toupper(pattern[i]);
- fg->pattern[fg->patternLen] = '\0';
+ fg->pattern[fg->len] = '\0';
} else
fg->pattern = (unsigned char *)pattern; /* really const */
/* Preprocess pattern. */
for (i = 0; i <= UCHAR_MAX; i++)
- fg->qsBc[i] = fg->patternLen;
- for (i = 1; i < fg->patternLen; i++) {
- fg->qsBc[fg->pattern[i]] = fg->patternLen - i;
+ fg->qsBc[i] = fg->len;
+ for (i = 1; i < fg->len; i++) {
+ fg->qsBc[fg->pattern[i]] = fg->len - i;
/*
* If case is ignored, make the jump apply to both upper and
* lower cased characters. As the pattern is stored in upper
* case, apply the same to the lower case equivalents.
*/
if (iflag)
- fg->qsBc[tolower(fg->pattern[i])] = fg->patternLen - i;
+ fg->qsBc[tolower(fg->pattern[i])] = fg->len - i;
}
}
@@ -97,29 +97,30 @@
int lastHalfDot = 0;
/* Initialize. */
- fg->patternLen = strlen(pattern);
+ fg->len = strlen(pattern);
fg->bol = 0;
fg->eol = 0;
- fg->reversedSearch = 0;
+ fg->reversed = 0;
/* Remove end-of-line character ('$'). */
- if (pattern[fg->patternLen - 1] == '$') {
+ if (pattern[fg->len - 1] == '$') {
eol++;
fg->eol = 1;
- fg->patternLen--;
+ fg->len--;
}
/* Remove beginning-of-line character ('^'). */
if (pattern[0] == '^') {
bol++;
fg->bol = 1;
- fg->patternLen--;
+ fg->len--;
}
- if (fg->patternLen >= 14 &&
+ if (fg->len >= 14 &&
strncmp(pattern + fg->bol, "[[:<:]]", 7) == 0 &&
- strncmp(pattern + fg->bol + fg->patternLen - 7, "[[:>:]]", 7) == 0) {
- fg->patternLen -= 14;
+ strncmp(pattern + fg->bol + fg->len - 7, "[[:>:]]", 7) == 0) {
+ fg->len -= 14;
+ /* Word boundary is handled separately in util.c */
wflag = 1;
}
@@ -128,12 +129,12 @@
* match character classes at the beginning and ending of the
* string respectively.
*/
- fg->pattern = grep_malloc(fg->patternLen + 1);
- memcpy(fg->pattern, pattern + bol + wflag, fg->patternLen);
- fg->pattern[fg->patternLen] = '\0';
+ fg->pattern = grep_malloc(fg->len + 1);
+ memcpy(fg->pattern, pattern + bol + wflag, fg->len);
+ fg->pattern[fg->len] = '\0';
/* Look for ways to cheat...er...avoid the full regex engine. */
- for (i = 0; i < fg->patternLen; i++)
+ for (i = 0; i < fg->len; i++)
{
/* Can still cheat? */
if ((isalnum(fg->pattern[i])) || isspace(fg->pattern[i]) ||
@@ -145,7 +146,7 @@
fg->pattern[i] = toupper(fg->pattern[i]);
} else if (fg->pattern[i] == '.') {
hasDot = i;
- if (i < fg->patternLen / 2) {
+ if (i < fg->len / 2) {
if (firstHalfDot < 0)
/* Closest dot to the beginning */
firstHalfDot = i;
@@ -169,11 +170,11 @@
*/
if ((!(lflag || cflag)) && ((!(bol || eol)) &&
((lastHalfDot) && ((firstHalfDot < 0) ||
- ((fg->patternLen - (lastHalfDot + 1)) < firstHalfDot)))) && !oflag && !color) {
- fg->reversedSearch = 1;
- hasDot = fg->patternLen - (firstHalfDot < 0 ?
+ ((fg->len - (lastHalfDot + 1)) < firstHalfDot)))) && !oflag && !color) {
+ fg->reversed = 1;
+ hasDot = fg->len - (firstHalfDot < 0 ?
firstLastHalfDot : firstHalfDot) - 1;
- grep_revstr(fg->pattern, fg->patternLen);
+ grep_revstr(fg->pattern, fg->len);
}
/*
@@ -196,105 +197,105 @@
*/
/* Adjust the shift based on location of the last dot ('.'). */
- shiftPatternLen = fg->patternLen - hasDot;
+ shiftPatternLen = fg->len - hasDot;
/* Preprocess pattern. */
for (i = 0; i <= UCHAR_MAX; i++)
fg->qsBc[i] = shiftPatternLen;
- for (i = hasDot + 1; i < fg->patternLen; i++) {
- fg->qsBc[fg->pattern[i]] = fg->patternLen - i;
+ for (i = hasDot + 1; i < fg->len; i++) {
+ fg->qsBc[fg->pattern[i]] = fg->len - i;
/*
* If case is ignored, make the jump apply to both upper and
* lower cased characters. As the pattern is stored in upper
* case, apply the same to the lower case equivalents.
*/
if (iflag)
- fg->qsBc[tolower(fg->pattern[i])] = fg->patternLen - i;
+ fg->qsBc[tolower(fg->pattern[i])] = fg->len - i;
}
/*
* Put pattern back to normal after pre-processing to allow for easy
* comparisons later.
*/
- if (fg->reversedSearch)
- grep_revstr(fg->pattern, fg->patternLen);
+ if (fg->reversed)
+ grep_revstr(fg->pattern, fg->len);
return (0);
}
int
-grep_search(fastgrep_t *fg, unsigned char *data, size_t dataLen, regmatch_t *pmatch)
+grep_search(fastgrep_t *fg, unsigned char *data, size_t len, regmatch_t *pmatch)
{
- int j;
- int rtrnVal = REG_NOMATCH;
+ int j;
+ int ret = REG_NOMATCH;
- if (pmatch->rm_so == dataLen)
- return (rtrnVal);
+ if (pmatch->rm_so == len)
+ return (ret);
if (fg->bol && pmatch->rm_so != 0) {
- pmatch->rm_so = dataLen;
- pmatch->rm_eo = dataLen;
- return (rtrnVal);
+ pmatch->rm_so = len;
+ pmatch->rm_eo = len;
+ return (ret);
}
/* No point in going farther if we do not have enough data. */
- if (dataLen < fg->patternLen)
- return (rtrnVal);
+ if (len < fg->len)
+ return (ret);
/* Only try once at the beginning or ending of the line. */
if (fg->bol || fg->eol) {
/* Simple text comparison. */
/* Verify data is >= pattern length before searching on it. */
- if (dataLen >= fg->patternLen) {
+ if (len >= fg->len) {
/* Determine where in data to start search at. */
if (fg->eol)
- j = dataLen - fg->patternLen;
+ j = len - fg->len;
else
j = 0;
- if (!((fg->bol && fg->eol) && (dataLen != fg->patternLen)))
+ if (!((fg->bol && fg->eol) && (len != fg->len)))
if (grep_cmp(fg->pattern, data + j,
- fg->patternLen) == -1) {
+ fg->len) == -1) {
pmatch->rm_so = j;
- pmatch->rm_eo = j + fg->patternLen;
- rtrnVal = 0;
+ pmatch->rm_eo = j + fg->len;
+ ret = 0;
}
}
- } else if (fg->reversedSearch) {
+ } else if (fg->reversed) {
/* Quick Search algorithm. */
- j = dataLen;
+ j = len;
do {
- if (grep_cmp(fg->pattern, data + j - fg->patternLen,
- fg->patternLen) == -1) {
- pmatch->rm_so = j - fg->patternLen;
+ if (grep_cmp(fg->pattern, data + j - fg->len,
+ fg->len) == -1) {
+ pmatch->rm_so = j - fg->len;
pmatch->rm_eo = j;
- rtrnVal = 0;
+ ret = 0;
break;
}
/* Shift if within bounds, otherwise, we are done. */
- if (j == fg->patternLen)
+ if (j == fg->len)
break;
- j -= fg->qsBc[data[j - fg->patternLen - 1]];
- } while (j >= fg->patternLen);
+ j -= fg->qsBc[data[j - fg->len - 1]];
+ } while (j >= fg->len);
} else {
/* Quick Search algorithm. */
j = pmatch->rm_so;
do {
- if (grep_cmp(fg->pattern, data + j, fg->patternLen) == -1) {
+ if (grep_cmp(fg->pattern, data + j, fg->len) == -1) {
pmatch->rm_so = j;
- pmatch->rm_eo = j + fg->patternLen;
- rtrnVal = 0;
+ pmatch->rm_eo = j + fg->len;
+ ret = 0;
break;
}
/* Shift if within bounds, otherwise, we are done. */
- if (j + fg->patternLen == dataLen)
+ if (j + fg->len == len)
break;
else
- j += fg->qsBc[data[j + fg->patternLen]];
- } while (j <= (dataLen - fg->patternLen));
+ j += fg->qsBc[data[j + fg->len]];
+ } while (j <= (len - fg->len));
}
- return (rtrnVal);
+ return (ret);
}
/*
@@ -304,7 +305,7 @@
static int
grep_cmp(const unsigned char *pattern, const unsigned char *data, size_t len)
{
- int i;
+ int i;
for (i = 0; i < len; i++) {
if (((pattern[i] == data[i]) || ((grepbehave != GREP_FIXED) && pattern[i] == '.'))
@@ -319,8 +320,8 @@
static void
grep_revstr(unsigned char *str, int len)
{
- int i;
- char c;
+ int i;
+ char c;
for (i = 0; i < len / 2; i++) {
c = str[i];
==== //depot/projects/soc2008/gabor_textproc/grep/grep.h#41 (text+ko) ====
@@ -93,13 +93,12 @@
typedef struct {
unsigned char *pattern;
- int patternLen;
+ int len;
int qsBc[UCHAR_MAX + 1];
/* flags */
int bol;
int eol;
- int wmatch;
- int reversedSearch;
+ int reversed;
} fastgrep_t;
/* Flags passed to regcomp() and regexec() */
More information about the p4-projects
mailing list