X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=diffcore-pickaxe.c;h=50e46ab863f71574622df4790ea33a8e521f2471;hb=459a21bd35675e140e19569e8b5c62c7fc4eee5b;hp=9cf3a5083852bff2e2bf09335e792b40af8e3d73;hpb=367cec1c024c3849cb32eaac15884a4adfefe1de;p=git.git diff --git a/diffcore-pickaxe.c b/diffcore-pickaxe.c index 9cf3a508..50e46ab8 100644 --- a/diffcore-pickaxe.c +++ b/diffcore-pickaxe.c @@ -4,21 +4,31 @@ #include "cache.h" #include "diff.h" #include "diffcore.h" -#include "delta.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)) + 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)