builtin-apply.c: push match-beginning/end logic down
This moves the logic to force match at the beginning and/or at the end of the buffer to the actual function that finds the match from its caller. This is a necessary preparation for the next step to allow matching disregarding certain differences, such as whitespace changes. We probably could optimize this even more by taking advantage of the fact that match_beginning and match_end forces the match to be at an exact location (anchored at the beginning and/or the end), but that's for another commit. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Jan 19, 2008 at 01:58 UTC
dc41976a3eed1d9c93fdc08c448bab969db4e0ec
1 file changed
+32
-14
builtin-apply.c
+32
-14
@@ -1439,18 +1439,36 @@ static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
1439
1440
static int match_fragment(const char *buf, unsigned long size,
1441
unsigned long try,
1442
- const char *fragment, unsigned long fragsize)
1442
+ const char *fragment, unsigned long fragsize,
1443
+ int match_beginning, int match_end)
1444
{
1444
- if (try + fragsize > size)
1445
+ if (match_beginning && try)
1446
return 0;
1446
- if (memcmp(buf + try, fragment, fragsize))
1447
- return 0;
1448
- return 1;
1447
+
1448
+ /*
1449
+ * Do we have an exact match? If we were told to match
1450
+ * at the end, size must be exactly at try+fragsize,
1451
+ * otherwise try+fragsize must be still within the preimage,
1452
+ * and either case, the old piece should match the preimage
1453
+ * exactly.
1454
+ */
1455
+ if ((match_end
1456
+ ? (try + fragsize == size)
1457
+ : (try + fragsize <= size)) &&
1458
+ !memcmp(buf + try, fragment, fragsize))
1459
+ return 1;
1460
+
1461
+ /*
1462
+ * NEEDSWORK: We can optionally match fuzzily here, but
1463
+ * that is for a later round.
1464
+ */
1465
+ return 0;
1466
}
1467
1468
static int find_offset(const char *buf, unsigned long size,
1469
const char *fragment, unsigned long fragsize,
1453
- int line, int *lines)
1470
+ int line, int *lines,
1471
+ int match_beginning, int match_end)
1472
{
1473
int i, no_more_backwards, no_more_forwards;
1474
unsigned long start, backwards, forwards, try;
@@ -1483,7 +1501,8 @@ static int find_offset(const char *buf, unsigned long size,
1501
no_more_backwards = !backwards;
1502
no_more_forwards = (forwards + fragsize > size);
1503
1486
- if (match_fragment(buf, size, try, fragment, fragsize)) {
1504
+ if (match_fragment(buf, size, try, fragment, fragsize,
1505
+ match_beginning, match_end)) {
1506
int shift = ((i+1) >> 1);
1507
if (i & 1)
1508
shift = -shift;
@@ -1765,17 +1784,16 @@ static int apply_one_fragment(struct strbuf *buf, struct fragment *frag,
1784
pos = frag->newpos;
1785
for (;;) {
1786
offset = find_offset(buf->buf, buf->len,
1768
- oldlines, oldsize, pos, &lines);
1769
- if (match_end && offset + oldsize != buf->len)
1770
- offset = -1;
1771
- if (match_beginning && offset)
1772
- offset = -1;
1787
+ oldlines, oldsize, pos, &lines,
1788
+ match_beginning, match_end);
1789
if (offset >= 0) {
1790
if (ws_error_action == correct_ws_error &&
1775
- (buf->len - oldsize - offset == 0)) /* end of file? */
1791
+ (buf->len - oldsize - offset == 0))
1792
+ /* end of file? */
1793
newsize -= new_blank_lines_at_end;
1794
1778
- /* Warn if it was necessary to reduce the number
1795
+ /*
1796
+ * Warn if it was necessary to reduce the number
1797
* of context lines.
1798
*/
1799
if ((leading != frag->leading) ||