changed find loop

master
Tibor Bizjak 2023-07-23 14:04:29 +02:00
parent bd26ae43a5
commit 8ebefb2974
1 changed files with 13 additions and 7 deletions

View File

@ -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;