Skip to content

Commit

Permalink
Merge branch 'rs/memmem'
Browse files Browse the repository at this point in the history
* rs/memmem:
  optimize compat/ memmem()
  diffcore-pickaxe: use memmem()
  • Loading branch information
Junio C Hamano committed Mar 11, 2009
2 parents e439979 + 56384e6 commit 8f4cc79
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
5 changes: 4 additions & 1 deletion compat/memmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down
18 changes: 8 additions & 10 deletions diffcore-pickaxe.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 8f4cc79

Please sign in to comment.