Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 19 Aug 2018 04:15:38 +0000 (UTC)
From:      Kyle Evans <kevans@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r338040 - head/usr.bin/diff
Message-ID:  <201808190415.w7J4Fc8s040203@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kevans
Date: Sun Aug 19 04:15:38 2018
New Revision: 338040
URL: https://svnweb.freebsd.org/changeset/base/338040

Log:
  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:
  head/usr.bin/diff/diffreg.c

Modified: head/usr.bin/diff/diffreg.c
==============================================================================
--- head/usr.bin/diff/diffreg.c	Sun Aug 19 03:57:20 2018	(r338039)
+++ head/usr.bin/diff/diffreg.c	Sun Aug 19 04:15:38 2018	(r338040)
@@ -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, 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
@@ -987,7 +1004,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;
 			}
 		}
@@ -995,36 +1012,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?201808190415.w7J4Fc8s040203>