From ae850b1ac755f471beac4dbfef4654fe3fbaaae9 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 9 Jan 2024 22:04:24 +0100 Subject: First commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libpatch_previous_hunk.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 libpatch_previous_hunk.c (limited to 'libpatch_previous_hunk.c') diff --git a/libpatch_previous_hunk.c b/libpatch_previous_hunk.c new file mode 100644 index 0000000..19ab964 --- /dev/null +++ b/libpatch_previous_hunk.c @@ -0,0 +1,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; +} -- cgit v1.2.3-70-g09d2