@matheusbsbs / matheus-bagliano / commits / 50fd6997c6
Fork 0

pickaxe: count regex matches only once

When --pickaxe-regex is used, forward past the end of matches instead of advancing to the byte after their start. This way matches count only once, even if the regular expression matches their tail -- like in the fixed-string fork of the code. E.g.: /.*/ used to count the number of bytes instead of the number of lines. /aa/ resulted in a count of two in "aaa" instead of one. Also document the fact that regexec() needs a NUL-terminated string as its second argument by adding an assert(). Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Mar 16, 2009 at 19:38 UTC 50fd6997c60f926efe4b3591f6c0a37568d02185
1 file changed +4 -2
diffcore-pickaxe.c
+4 -2
@@ -25,10 +25,12 @@ static unsigned int contains(struct diff_filespec *one,
25 regmatch_t regmatch;
26 int flags = 0;
27
28 + assert(data[sz] == '\0');
29 while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
30 flags |= REG_NOTBOL;
30 - data += regmatch.rm_so;
31 - if (*data) data++;
31 + data += regmatch.rm_eo;
32 + if (*data && regmatch.rm_so == regmatch.rm_eo)
33 + data++;
34 cnt++;
35 }
36