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

builtin-apply.c: move copy_wsfix() function a bit higher.

I'll be calling this from match_fragment() in later rounds. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jan 30, 2008 at 15:11 UTC ee810b715912347775fa06a0d5b16092b4908d66
1 file changed +94 -94
builtin-apply.c
+94 -94
@@ -1515,6 +1515,100 @@ static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
1515 }
1516 }
1517
1518 +static int copy_wsfix(char *output, const char *patch, int plen,
1519 + unsigned ws_rule)
1520 +{
1521 + /*
1522 + * plen is number of bytes to be copied from patch, starting
1523 + * at patch. Typically patch[plen-1] is '\n', unless this is
1524 + * the incomplete last line.
1525 + */
1526 + int i;
1527 + int add_nl_to_tail = 0;
1528 + int fixed = 0;
1529 + int last_tab_in_indent = -1;
1530 + int last_space_in_indent = -1;
1531 + int need_fix_leading_space = 0;
1532 + char *buf;
1533 +
1534 + /*
1535 + * Strip trailing whitespace
1536 + */
1537 + if ((ws_rule & WS_TRAILING_SPACE) &&
1538 + (2 < plen && isspace(patch[plen-2]))) {
1539 + if (patch[plen-1] == '\n')
1540 + add_nl_to_tail = 1;
1541 + plen--;
1542 + while (0 < plen && isspace(patch[plen-1]))
1543 + plen--;
1544 + fixed = 1;
1545 + }
1546 +
1547 + /*
1548 + * Check leading whitespaces (indent)
1549 + */
1550 + for (i = 0; i < plen; i++) {
1551 + char ch = patch[i];
1552 + if (ch == '\t') {
1553 + last_tab_in_indent = i;
1554 + if ((ws_rule & WS_SPACE_BEFORE_TAB) &&
1555 + 0 <= last_space_in_indent)
1556 + need_fix_leading_space = 1;
1557 + } else if (ch == ' ') {
1558 + last_space_in_indent = i;
1559 + if ((ws_rule & WS_INDENT_WITH_NON_TAB) &&
1560 + 8 <= i - last_tab_in_indent)
1561 + need_fix_leading_space = 1;
1562 + } else
1563 + break;
1564 + }
1565 +
1566 + buf = output;
1567 + if (need_fix_leading_space) {
1568 + /* Process indent ourselves */
1569 + int consecutive_spaces = 0;
1570 + int last = last_tab_in_indent + 1;
1571 +
1572 + if (ws_rule & WS_INDENT_WITH_NON_TAB) {
1573 + /* have "last" point at one past the indent */
1574 + if (last_tab_in_indent < last_space_in_indent)
1575 + last = last_space_in_indent + 1;
1576 + else
1577 + last = last_tab_in_indent + 1;
1578 + }
1579 +
1580 + /*
1581 + * between patch[0..last-1], strip the funny spaces,
1582 + * updating them to tab as needed.
1583 + */
1584 + for (i = 0; i < last; i++) {
1585 + char ch = patch[i];
1586 + if (ch != ' ') {
1587 + consecutive_spaces = 0;
1588 + *output++ = ch;
1589 + } else {
1590 + consecutive_spaces++;
1591 + if (consecutive_spaces == 8) {
1592 + *output++ = '\t';
1593 + consecutive_spaces = 0;
1594 + }
1595 + }
1596 + }
1597 + while (0 < consecutive_spaces--)
1598 + *output++ = ' ';
1599 + plen -= last;
1600 + patch += last;
1601 + fixed = 1;
1602 + }
1603 +
1604 + memcpy(output, patch, plen);
1605 + if (add_nl_to_tail)
1606 + output[plen++] = '\n';
1607 + if (fixed)
1608 + applied_after_fixing_ws++;
1609 + return output + plen - buf;
1610 +}
1611 +
1612 static int match_fragment(struct image *img,
1613 struct image *preimage,
1614 struct image *postimage,
@@ -1642,100 +1736,6 @@ static void remove_last_line(struct image *img)
1736 img->len -= img->line[--img->nr].len;
1737 }
1738
1645 -static int copy_wsfix(char *output, const char *patch, int plen,
1646 - unsigned ws_rule)
1647 -{
1648 - /*
1649 - * plen is number of bytes to be copied from patch, starting
1650 - * at patch. Typically patch[plen-1] is '\n', unless this is
1651 - * the incomplete last line.
1652 - */
1653 - int i;
1654 - int add_nl_to_tail = 0;
1655 - int fixed = 0;
1656 - int last_tab_in_indent = -1;
1657 - int last_space_in_indent = -1;
1658 - int need_fix_leading_space = 0;
1659 - char *buf;
1660 -
1661 - /*
1662 - * Strip trailing whitespace
1663 - */
1664 - if ((ws_rule & WS_TRAILING_SPACE) &&
1665 - (2 < plen && isspace(patch[plen-2]))) {
1666 - if (patch[plen-1] == '\n')
1667 - add_nl_to_tail = 1;
1668 - plen--;
1669 - while (0 < plen && isspace(patch[plen-1]))
1670 - plen--;
1671 - fixed = 1;
1672 - }
1673 -
1674 - /*
1675 - * Check leading whitespaces (indent)
1676 - */
1677 - for (i = 0; i < plen; i++) {
1678 - char ch = patch[i];
1679 - if (ch == '\t') {
1680 - last_tab_in_indent = i;
1681 - if ((ws_rule & WS_SPACE_BEFORE_TAB) &&
1682 - 0 <= last_space_in_indent)
1683 - need_fix_leading_space = 1;
1684 - } else if (ch == ' ') {
1685 - last_space_in_indent = i;
1686 - if ((ws_rule & WS_INDENT_WITH_NON_TAB) &&
1687 - 8 <= i - last_tab_in_indent)
1688 - need_fix_leading_space = 1;
1689 - } else
1690 - break;
1691 - }
1692 -
1693 - buf = output;
1694 - if (need_fix_leading_space) {
1695 - /* Process indent ourselves */
1696 - int consecutive_spaces = 0;
1697 - int last = last_tab_in_indent + 1;
1698 -
1699 - if (ws_rule & WS_INDENT_WITH_NON_TAB) {
1700 - /* have "last" point at one past the indent */
1701 - if (last_tab_in_indent < last_space_in_indent)
1702 - last = last_space_in_indent + 1;
1703 - else
1704 - last = last_tab_in_indent + 1;
1705 - }
1706 -
1707 - /*
1708 - * between patch[0..last-1], strip the funny spaces,
1709 - * updating them to tab as needed.
1710 - */
1711 - for (i = 0; i < last; i++) {
1712 - char ch = patch[i];
1713 - if (ch != ' ') {
1714 - consecutive_spaces = 0;
1715 - *output++ = ch;
1716 - } else {
1717 - consecutive_spaces++;
1718 - if (consecutive_spaces == 8) {
1719 - *output++ = '\t';
1720 - consecutive_spaces = 0;
1721 - }
1722 - }
1723 - }
1724 - while (0 < consecutive_spaces--)
1725 - *output++ = ' ';
1726 - plen -= last;
1727 - patch += last;
1728 - fixed = 1;
1729 - }
1730 -
1731 - memcpy(output, patch, plen);
1732 - if (add_nl_to_tail)
1733 - output[plen++] = '\n';
1734 - if (fixed)
1735 - applied_after_fixing_ws++;
1736 - return output + plen - buf;
1737 -}
1738 -
1739 static void update_image(struct image *img,
1740 int applied_pos,
1741 struct image *preimage,