threads / patch / 1322

patchdiffcore-pickaxe: switch to "counting" behaviour.

Subject: [PATCH] diffcore-pickaxe: switch to "counting" behaviour.

## tl;dr

One message between Jul 23, 2005 and Jul 23, 2005. Diffs are folded; open one to read it.

replies: 0people: 1as markdown or json

Junio C Hamano· Jul 23, 2005, 23:35 UTC · lore

Instead of finding old/new pair that one side has and the other side does not have the specified string, find old/new pair that contains the specified string as a substring different number of times. This would still not catch a case where you introduce two static variable declarations and remove two static function definitions from a file with -S"static", but would make it behave a bit more intuitively.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---
*** This was brought up as a possible improvement for the first
*** question when you asked me two questions a couple of weeks
*** ago.  Interestingly enough, Paul Mackerras independently
*** suggested the same approach to make pickaxe more intuitive
*** for gitk users.
 diffcore-pickaxe.c |   23 +++++++++++++++++------
 1 files changed, 17 insertions(+), 6 deletions(-)
d245f19da1e36a70e904f2414d815fc06bfe09d8
Show changes to diffcore-pickaxe.c +17 −6
diff --git a/diffcore-pickaxe.c b/diffcore-pickaxe.c
--- a/diffcore-pickaxe.c
+++ b/diffcore-pickaxe.c
@@ -5,19 +5,30 @@
 #include "diff.h"
 #include "diffcore.h"
 
-static int contains(struct diff_filespec *one,
-		    const char *needle, unsigned long len)
+static unsigned int contains(struct diff_filespec *one,
+			     const char *needle, unsigned long len)
 {
+	unsigned int cnt;
 	unsigned long offset, sz;
 	const char *data;
 	if (diff_populate_filespec(one, 0))
 		return 0;
+
 	sz = one->size;
 	data = one->data;
-	for (offset = 0; offset + len <= sz; offset++)
-		     if (!strncmp(needle, data + offset, len))
-			     return 1;
-	return 0;
+	cnt = 0;
+
+	/* Yes, I've heard of strstr(), but the thing is *data may
+	 * not be NUL terminated.  Sue me.
+	 */
+	for (offset = 0; offset + len <= sz; offset++) {
+		/* we count non-overlapping occurrences of needle */
+		if (!memcmp(needle, data + offset, len)) {
+			offset += len - 1;
+			cnt++;
+		}
+	}
+	return cnt;
 }
 
 void diffcore_pickaxe(const char *needle, int opts)

← back to recent threads