Skip to content

Commit 2aa93de

Browse files
committed
Merge branch 'rs/memmem' into maint
* rs/memmem: optimize compat/ memmem() diffcore-pickaxe: use memmem()
2 parents 10a73f5 + 56384e6 commit 2aa93de

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

compat/memmem.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ void *gitmemmem(const void *haystack, size_t haystack_len,
55
{
66
const char *begin = haystack;
77
const char *last_possible = begin + haystack_len - needle_len;
8+
const char *tail = needle;
9+
char point;
810

911
/*
1012
* 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,
2022
if (haystack_len < needle_len)
2123
return NULL;
2224

25+
point = *tail++;
2326
for (; begin <= last_possible; begin++) {
24-
if (!memcmp(begin, needle, needle_len))
27+
if (*begin == point && !memcmp(begin + 1, tail, needle_len - 1))
2528
return (void *)begin;
2629
}
2730

diffcore-pickaxe.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static unsigned int contains(struct diff_filespec *one,
1010
regex_t *regexp)
1111
{
1212
unsigned int cnt;
13-
unsigned long offset, sz;
13+
unsigned long sz;
1414
const char *data;
1515
if (diff_populate_filespec(one, 0))
1616
return 0;
@@ -33,15 +33,13 @@ static unsigned int contains(struct diff_filespec *one,
3333
}
3434

3535
} else { /* Classic exact string match */
36-
/* Yes, I've heard of strstr(), but the thing is *data may
37-
* not be NUL terminated. Sue me.
38-
*/
39-
for (offset = 0; offset + len <= sz; offset++) {
40-
/* we count non-overlapping occurrences of needle */
41-
if (!memcmp(needle, data + offset, len)) {
42-
offset += len - 1;
43-
cnt++;
44-
}
36+
while (sz) {
37+
const char *found = memmem(data, sz, needle, len);
38+
if (!found)
39+
break;
40+
sz -= found - data + len;
41+
data = found + len;
42+
cnt++;
4543
}
4644
}
4745
diff_free_filespec_data(one);

0 commit comments

Comments
 (0)