[dev] [PATCH 2/2] diff: changes edits are grouped automatically since we do not recognise substitution

From: <maandree_AT_kth.se>
Date: Thu, 28 Jan 2016 00:18:37 +0100

Signed-off-by: Mattias Andrée <maandree_AT_kth.se>
---
 diff.c | 50 +++++++++++++++-----------------------------------
 1 file changed, 15 insertions(+), 35 deletions(-)
diff --git a/diff.c b/diff.c
index 27ad8cf..cd0166c 100644
--- a/diff.c
+++ b/diff.c
_AT_@ -107,9 +107,9 @@ diff2(char **a, char **b, size_t an, size_t bn)
 			size_t d = matrix[ai - 1][bi - 1];
 			size_t u = matrix[ai - 1][bi    ];
 			size_t l = matrix[ai    ][bi - 1];
-			size_t lu = 1 + (l < u ? l : u);
+			size_t lu = 1 + (l <= u ? l : u);
 			int ch = strcmp(a[ai], b[bi]); /* remember !! if using d += ch */
-			map   [ai][bi] = (lu < d || ch) ? 1 + (l < u) : 0;
+			map   [ai][bi] = (lu < d || ch) ? 1 + (l <= u) : 0;
 			matrix[ai][bi] = (lu < d || ch) ? lu : d;
 			if (matrix[ai][bi] < matrix[ai][boff]) {
 				boff = bi - 1;
_AT_@ -131,26 +131,6 @@ diff2(char **a, char **b, size_t an, size_t bn)
 	return rc + ri;
 }
 
-static void
-flush_output(struct output *output, size_t n)
-{
-	size_t i;
-
-	if (!n)
-		return;
-
-	if (output->colour == 0) {
-		printf("%s%s\n", " ", output->text);
-	} else {
-		for (i = 0; i < n; i++)
-			if (output[i].colour == 1)
-				printf("\033[31m-%s\033[m\n", output[i].text);
-		for (i = 0; i < n; i++)
-			if (output[i].colour == 2)
-				printf("\033[32m+%s\033[m\n", output[i].text);
-	}
-}
-
 int
 main(int argc, char *argv[])
 {
_AT_@ -158,8 +138,7 @@ main(int argc, char *argv[])
 	struct file_data *new;
 	char *path;
 	char trace;
-	struct output *output;
-	size_t n, ai, bi;
+	size_t ai, bi;
 	char **a;
 	char **b;
 	int ret = 0;
_AT_@ -171,20 +150,22 @@ main(int argc, char *argv[])
 
 	a = old->lines, b = new->lines;
 	path = diff2(a, b, old->line_count, new->line_count);
-	
-	output = emalloc((old->line_count + new->line_count) * sizeof(*output));
-	for (n = ai = bi = 0; (trace = *--path) != END_OF_PATH;) {
-		if (trace) {
-			output[n++] = (struct output){ trace, trace == 1 ? a[ai] : b[bi] };
-		} else {
-			flush_output(output, n), n = 0;
-			output[n++] = (struct output){ trace, a[ai] };
-			flush_output(output, n), n = 0;
+
+	for (ai = bi = 0; (trace = *--path) != END_OF_PATH;) {
+		switch (trace) {
+		default:
+			printf("%s%s\n", " ", a[ai]);
+			break;
+		case 1:
+			printf("\033[31m-%s\033[m\n", a[ai]);
+			break;
+		case 2:
+			printf("\033[32m+%s\033[m\n", b[bi]);
+			break;
 		}
 		ai += trace != 2;
 		bi += trace != 1;
 	}
-	flush_output(output, n);
 
 	if (fshut(stdout, "<stdout>"))
 		ret = 2;
_AT_@ -192,6 +173,5 @@ main(int argc, char *argv[])
 	free(old);
 	free(new);
 	free(path);
-	free(output);
 	return ret;
 }
-- 
2.7.0
Received on Thu Jan 28 2016 - 00:18:37 CET

This archive was generated by hypermail 2.3.0 : Thu Jan 28 2016 - 00:24:19 CET