aboutsummaryrefslogtreecommitdiffstats
path: root/libpatch_previous_hunk.c
blob: 19ab9645cdf8db5165e822a32309ee7a0dc43640 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* See LICENSE file for copyright and license details. */
#include "common.h"


size_t
libpatch_previous_hunk(struct libpatch_diff2 *diff, size_t *position,
                       size_t *ai, size_t *bi, size_t *an_out, size_t *bn_out, int skip_context)
{
	size_t n;
	int mask = skip_context ? 0x03 : ~0;

	while (*position && !(diff[*position - 1].change & mask)) {
		*position -= 1;
		*ai -= (size_t)diff[*position].repetition;
		*bi -= (size_t)diff[*position].repetition;
	}

	*an_out = 0;
	*bn_out = 0;
	n = 0;

	while (*position && (diff[*position - 1].change & mask)) {
		*position -= 1;
		n += 1;
		if (diff[*position].change != LIBPATCH_DIFF2_FILE2_ONLY)
			*an_out += (size_t)diff[*position].repetition;
		if (diff[*position].change != LIBPATCH_DIFF2_FILE1_ONLY)
			*bn_out += (size_t)diff[*position].repetition;
	}

	*ai -= *an_out;
	*bi -= *bn_out;
	return n;
}