Merge branch 'rs/memmem'

* rs/memmem:
  optimize compat/ memmem()
  diffcore-pickaxe: use memmem()
This commit is contained in:
Junio C Hamano
2009-03-11 13:49:42 -07:00
2 changed files with 12 additions and 11 deletions

View File

@ -5,6 +5,8 @@ void *gitmemmem(const void *haystack, size_t haystack_len,
{ {
const char *begin = haystack; const char *begin = haystack;
const char *last_possible = begin + haystack_len - needle_len; 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 * 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) if (haystack_len < needle_len)
return NULL; return NULL;
point = *tail++;
for (; begin <= last_possible; begin++) { for (; begin <= last_possible; begin++) {
if (!memcmp(begin, needle, needle_len)) if (*begin == point && !memcmp(begin + 1, tail, needle_len - 1))
return (void *)begin; return (void *)begin;
} }

View File

@ -10,7 +10,7 @@ static unsigned int contains(struct diff_filespec *one,
regex_t *regexp) regex_t *regexp)
{ {
unsigned int cnt; unsigned int cnt;
unsigned long offset, sz; unsigned long sz;
const char *data; const char *data;
if (diff_populate_filespec(one, 0)) if (diff_populate_filespec(one, 0))
return 0; return 0;
@ -33,15 +33,13 @@ static unsigned int contains(struct diff_filespec *one,
} }
} else { /* Classic exact string match */ } else { /* Classic exact string match */
/* Yes, I've heard of strstr(), but the thing is *data may while (sz) {
* not be NUL terminated. Sue me. const char *found = memmem(data, sz, needle, len);
*/ if (!found)
for (offset = 0; offset + len <= sz; offset++) { break;
/* we count non-overlapping occurrences of needle */ sz -= found - data + len;
if (!memcmp(needle, data + offset, len)) { data = found + len;
offset += len - 1; cnt++;
cnt++;
}
} }
} }
diff_free_filespec_data(one); diff_free_filespec_data(one);