* Trim down common substring at the end of the buffers,
* but leave at least ctx lines at the end.
*/
-static void trim_common_tail(mmfile_t *a, mmfile_t *b, int ctx)
+static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx)
{
const int blk = 1024;
long trimmed = 0, recovered = 0;
- int i;
char *ap = a->ptr + a->size;
char *bp = b->ptr + b->size;
long smaller = (a->size < b->size) ? a->size : b->size;
+ if (ctx)
+ return;
+
while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
trimmed += blk;
ap -= blk;
bp -= blk;
}
- for (i = 0, recovered = 0; recovered < trimmed && i <= ctx; i++) {
- while (recovered < trimmed && ap[recovered] != '\n')
- recovered++;
- }
- a->size -= (trimmed - recovered);
- b->size -= (trimmed - recovered);
+ while (recovered < trimmed)
+ if (ap[recovered++] == '\n')
+ break;
+ a->size -= trimmed - recovered;
+ b->size -= trimmed - recovered;
}
int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
if ((f = fopen(filename, "rb")) == NULL)
return error("Could not open %s", filename);
sz = xsize_t(st.st_size);
- ptr->ptr = xmalloc(sz);
- if (fread(ptr->ptr, sz, 1, f) != 1)
+ ptr->ptr = xmalloc(sz ? sz : 1);
+ if (sz && fread(ptr->ptr, sz, 1, f) != 1)
return error("Could not read %s", filename);
fclose(f);
ptr->size = sz;
expression = value;
if (regcomp(®->re, expression, 0))
die("Invalid regexp to look for hunk header: %s", expression);
- if (buffer)
- free(buffer);
+ free(buffer);
value = ep + 1;
}
}