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
|
/* find: prints lines containing pattern, returns number of matching lines
|
||||||
* gracefully handle line overflow
|
* gracefully handle line overflow
|
||||||
* compile with -O3 */
|
* 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;
|
extern long lineno;
|
||||||
int i, c, found;
|
int c, found = 0;
|
||||||
const int plen = strlen(pattern);
|
const char * const pstart = pattern;
|
||||||
i = found = 0;
|
|
||||||
|
|
||||||
while ((c = getch_lnbuff()) != EOF) {
|
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();
|
ignoreln_lnbuff();
|
||||||
else if (except_flag ? c == '\n' : i == plen) { // match
|
else if (match > 0) { // match
|
||||||
found++;
|
found++;
|
||||||
if (lineno_flag)
|
if (lineno_flag)
|
||||||
printf("%ld:", lineno);
|
printf("%ld:", lineno);
|
||||||
flush_lnbuff();
|
flush_lnbuff();
|
||||||
}
|
}
|
||||||
|
pattern = pstart;
|
||||||
}
|
}
|
||||||
|
|
||||||
return found;
|
return found;
|
||||||
|
|
Loading…
Reference in New Issue