@matheusbsbs / matheus-bagliano / commits / c89fb6b19a
Fork 0

builtin-apply.c: refactor small part that matches context

This moves three "if" conditions out of line from find_offset() function, which is responsible for finding the matching place in the preimage to apply the patch. There is no change in the logic of the program. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jan 19, 2008 at 00:42 UTC c89fb6b19a91add28a111cf257e01cba2c8de69c
1 file changed +13 -5
builtin-apply.c
+13 -5
@@ -1437,6 +1437,17 @@ static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
1437 }
1438 }
1439
1440 +static int match_fragment(const char *buf, unsigned long size,
1441 + unsigned long try,
1442 + const char *fragment, unsigned long fragsize)
1443 +{
1444 + if (try + fragsize > size)
1445 + return 0;
1446 + if (memcmp(buf + try, fragment, fragsize))
1447 + return 0;
1448 + return 1;
1449 +}
1450 +
1451 static int find_offset(const char *buf, unsigned long size,
1452 const char *fragment, unsigned long fragsize,
1453 int line, int *lines)
@@ -1461,8 +1472,7 @@ static int find_offset(const char *buf, unsigned long size,
1472 }
1473
1474 /* Exact line number? */
1464 - if ((start + fragsize <= size) &&
1465 - !memcmp(buf + start, fragment, fragsize))
1475 + if (match_fragment(buf, size, start, fragment, fragsize))
1476 return start;
1477
1478 /*
@@ -1494,9 +1504,7 @@ static int find_offset(const char *buf, unsigned long size,
1504 try = forwards;
1505 }
1506
1497 - if (try + fragsize > size)
1498 - continue;
1499 - if (memcmp(buf + try, fragment, fragsize))
1507 + if (!match_fragment(buf, size, try, fragment, fragsize))
1508 continue;
1509 n = (i >> 1)+1;
1510 if (i & 1)