summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Junio C Hamano <gitster@pobox.com>2009-03-11 13:49:42 -0700
committerLibravatar Junio C Hamano <gitster@pobox.com>2009-03-11 13:49:42 -0700
commit8f4cc7911955a3642af601394a4b4473e7bfde33 (patch)
tree3c692d0737f27703365c0e2b495259e0636b2697
parentMerge branch 'tv/rebase-stat' (diff)
parentoptimize compat/ memmem() (diff)
downloadtgif-8f4cc7911955a3642af601394a4b4473e7bfde33.tar.xz
Merge branch 'rs/memmem'
* rs/memmem: optimize compat/ memmem() diffcore-pickaxe: use memmem()
-rw-r--r--compat/memmem.c5
-rw-r--r--diffcore-pickaxe.c18
2 files changed, 12 insertions, 11 deletions
diff --git a/compat/memmem.c b/compat/memmem.c
index cd0d877364..56bcb4277f 100644
--- a/compat/memmem.c
+++ b/compat/memmem.c
@@ -5,6 +5,8 @@ void *gitmemmem(const void *haystack, size_t haystack_len,
{
const char *begin = haystack;
const char *last_possible = begin + haystack_len - needle_len;
+ const char *tail = needle;
+ char point;
/*
* The first occurrence of the empty string is deemed to occur at
@@ -20,8 +22,9 @@ void *gitmemmem(const void *haystack, size_t haystack_len,
if (haystack_len < needle_len)
return NULL;
+ point = *tail++;
for (; begin <= last_possible; begin++) {
- if (!memcmp(begin, needle, needle_len))
+ if (*begin == point && !memcmp(begin + 1, tail, needle_len - 1))
return (void *)begin;
}
diff --git a/diffcore-pickaxe.c b/diffcore-pickaxe.c
index af9fffe6e8..574b3e8337 100644
--- a/diffcore-pickaxe.c
+++ b/diffcore-pickaxe.c
@@ -10,7 +10,7 @@ static unsigned int contains(struct diff_filespec *one,
regex_t *regexp)
{
unsigned int cnt;
- unsigned long offset, sz;
+ unsigned long sz;
const char *data;
if (diff_populate_filespec(one, 0))
return 0;
@@ -33,15 +33,13 @@ static unsigned int contains(struct diff_filespec *one,
}
} else { /* Classic exact string match */
- /* 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++;
- }
+ while (sz) {
+ const char *found = memmem(data, sz, needle, len);
+ if (!found)
+ break;
+ sz -= found - data + len;
+ data = found + len;
+ cnt++;
}
}
diff_free_filespec_data(one);