Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 3 Oct 2018 17:21:46 +0000 (UTC)
From:      Kyle Evans <kevans@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org
Subject:   svn commit: r339164 - stable/11/usr.bin/diff
Message-ID:  <201810031721.w93HLkcG015250@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kevans
Date: Wed Oct  3 17:21:45 2018
New Revision: 339164
URL: https://svnweb.freebsd.org/changeset/base/339164

Log:
  MFC r338040: 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.

Modified:
  stable/11/usr.bin/diff/diffreg.c
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/usr.bin/diff/diffreg.c
==============================================================================
--- stable/11/usr.bin/diff/diffreg.c	Wed Oct  3 17:20:30 2018	(r339163)
+++ stable/11/usr.bin/diff/diffreg.c	Wed Oct  3 17:21:45 2018	(r339164)
@@ -202,7 +202,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);
@@ -1018,8 +1019,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;
 
@@ -1028,6 +1029,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
@@ -1043,12 +1058,14 @@ change(char *file1, FILE *f1, char *file2, FILE *f2, i
 	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
@@ -1059,7 +1076,7 @@ restart:
 			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;
 			}
 		}
@@ -1067,36 +1084,11 @@ restart:
 			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) {



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201810031721.w93HLkcG015250>