diff(1): Refactor -B a little bit

Instead of doing a second pass to skip empty lines if we've specified -I, go
ahead and check both at once. Ignore critera has been split out into its own
function to try and keep the logic cleaner.
This commit is contained in:
Kyle Evans 2018-08-19 04:15:38 +00:00
parent e68edb8cf0
commit 9488de009c

View File

@ -196,7 +196,8 @@ static void unsort(struct line *, int, int *);
static void change(char *, FILE *, char *, FILE *, int, int, int, int, int *);
static void sort(struct line *, int);
static void print_header(const char *, const char *);
static int ignoreline(char *);
static bool ignoreline_pattern(char *);
static bool ignoreline(char *, bool);
static int asciifile(FILE *);
static int fetch(long *, int, int, FILE *, int, int, int);
static int newcand(int, int, int);
@ -946,8 +947,8 @@ preadline(int fd, size_t rlen, off_t off)
return (line);
}
static int
ignoreline(char *line)
static bool
ignoreline_pattern(char *line)
{
int ret;
@ -956,6 +957,20 @@ ignoreline(char *line)
return (ret == 0); /* if it matched, it should be ignored. */
}
static bool
ignoreline(char *line, bool skip_blanks)
{
if (ignore_pats != NULL && skip_blanks)
return (ignoreline_pattern(line) || *line == '\0');
if (ignore_pats != NULL)
return (ignoreline_pattern(line));
if (skip_blanks)
return (*line == '\0');
/* No ignore criteria specified */
return (false);
}
/*
* Indicate that there is a difference between lines a and b of the from file
* to get to lines c to d of the to file. If a is greater then b then there
@ -971,12 +986,14 @@ change(char *file1, FILE *f1, char *file2, FILE *f2, int a, int b, int c, int d,
long curpos;
int i, nc, f;
const char *walk;
bool skip_blanks;
skip_blanks = (*pflags & D_SKIPBLANKLINES);
restart:
if ((diff_format != D_IFDEF || diff_format == D_GFORMAT) &&
a > b && c > d)
return;
if (ignore_pats != NULL) {
if (ignore_pats != NULL || skip_blanks) {
char *line;
/*
* All lines in the change, insert, or delete must
@ -987,7 +1004,7 @@ change(char *file1, FILE *f1, char *file2, FILE *f2, int a, int b, int c, int d,
for (i = a; i <= b; i++) {
line = preadline(fileno(f1),
ixold[i] - ixold[i - 1], ixold[i - 1]);
if (!ignoreline(line))
if (!ignoreline(line, skip_blanks))
goto proceed;
}
}
@ -995,37 +1012,12 @@ change(char *file1, FILE *f1, char *file2, FILE *f2, int a, int b, int c, int d,
for (i = c; i <= d; i++) {
line = preadline(fileno(f2),
ixnew[i] - ixnew[i - 1], ixnew[i - 1]);
if (!ignoreline(line))
if (!ignoreline(line, skip_blanks))
goto proceed;
}
}
return;
}
if (*pflags & D_SKIPBLANKLINES) {
char *line;
/*
* All lines in the change, insert, or delete must not be
* empty for the change to be ignored.
*/
if (a <= b) { /* Changes and deletes. */
for (i = a; i <= b; i++) {
line = preadline(fileno(f1),
ixold[i] - ixold[i - 1], ixold[i - 1]);
if (*line != '\0')
goto proceed;
}
}
if (a > b || c <= d) { /* Changes and inserts. */
for (i = c; i <= d; i++) {
line = preadline(fileno(f2),
ixnew[i] - ixnew[i - 1], ixnew[i - 1]);
if (*line != '\0')
goto proceed;
}
}
return;
}
proceed:
if (*pflags & D_HEADER && diff_format != D_BRIEF) {
diff_output("%s %s %s\n", diffargs, file1, file2);