changed find loop
parent
bd26ae43a5
commit
8ebefb2974
|
@ -9,25 +9,31 @@ void ignoreln_lnbuff(void);
|
|||
/* find: prints lines containing pattern, returns number of matching lines
|
||||
* gracefully handle line overflow
|
||||
* compile with -O3 */
|
||||
int find(char *pattern, char lineno_flag, char except_flag)
|
||||
int find(const char *pattern, char lineno_flag, char except_flag)
|
||||
{
|
||||
|
||||
extern long lineno;
|
||||
int i, c, found;
|
||||
const int plen = strlen(pattern);
|
||||
i = found = 0;
|
||||
int c, found = 0;
|
||||
const char * const pstart = pattern;
|
||||
|
||||
while ((c = getch_lnbuff()) != EOF) {
|
||||
i = (c == pattern[i]) ? i+1 : 0;
|
||||
if (c == *pattern) {
|
||||
pattern++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (except_flag && i == plen)
|
||||
char match = !*pattern - (c == '\n');
|
||||
match *= except_flag ? -1 : 1;
|
||||
|
||||
if (match < 0 && except_flag) // no match
|
||||
ignoreln_lnbuff();
|
||||
else if (except_flag ? c == '\n' : i == plen) { // match
|
||||
else if (match > 0) { // match
|
||||
found++;
|
||||
if (lineno_flag)
|
||||
printf("%ld:", lineno);
|
||||
flush_lnbuff();
|
||||
}
|
||||
pattern = pstart;
|
||||
}
|
||||
|
||||
return found;
|
||||
|
|
Loading…
Reference in New Issue