+/*
+ * 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)
+{
+ 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;
+
+ 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);
+}
+