1629
strbuf_release(&title);
1630
}
1631
1632
+static int pp_utf8_width(const char *start, const char *end)
1633
+{
1634
+ int width = 0;
1635
+ size_t remain = end - start;
1636
+
1637
+ while (remain) {
1638
+ int n = utf8_width(&start, &remain);
1639
+ if (n < 0 || !start)
1640
+ return -1;
1641
+ width += n;
1642
+ }
1643
+ return width;
1644
+}
1645
+
1646
+static void strbuf_add_tabexpand(struct strbuf *sb,
1647
+ const char *line, int linelen)
1648
+{
1649
+ const char *tab;
1650
+
1651
+ while ((tab = memchr(line, '\t', linelen)) != NULL) {
1652
+ int width = pp_utf8_width(line, tab);
1653
+
1654
+ /*
1655
+ * If it wasn't well-formed utf8, or it
1656
+ * had characters with badly defined
1657
+ * width (control characters etc), just
1658
+ * give up on trying to align things.
1659
+ */
1660
+ if (width < 0)
1661
+ break;
1662
+
1663
+ /* Output the data .. */
1664
+ strbuf_add(sb, line, tab - line);
1665
+
1666
+ /* .. and the de-tabified tab */
1667
+ strbuf_addchars(sb, ' ', 8 - (width % 8));
1668
+
1669
+ /* Skip over the printed part .. */
1670
+ linelen -= tab + 1 - line;
1671
+ line = tab + 1;
1672
+ }
1673
+
1674
+ /*
1675
+ * Print out everything after the last tab without
1676
+ * worrying about width - there's nothing more to
1677
+ * align.
1678
+ */
1679
+ strbuf_add(sb, line, linelen);
1680
+}
1681
+
1682
+/*
1683
+ * pp_handle_indent() prints out the intendation, and
1684
+ * the whole line (without the final newline), after
1685
+ * de-tabifying.
1686
+ */
1687
+static void pp_handle_indent(struct pretty_print_context *pp,
1688
+ struct strbuf *sb, int indent,
1689
+ const char *line, int linelen)
1690
+{
1691
+ strbuf_addchars(sb, ' ', indent);
1692
+ if (pp->expand_tabs_in_log)
1693
+ strbuf_add_tabexpand(sb, line, linelen);
1694
+ else
1695
+ strbuf_add(sb, line, linelen);
1696
+}
1697
+
1698
void pp_remainder(struct pretty_print_context *pp,
1699
const char **msg_p,
1700
struct strbuf *sb,
1719
1720
strbuf_grow(sb, linelen + indent + 20);
1721
if (indent)
1656
- strbuf_addchars(sb, ' ', indent);
1657
- strbuf_add(sb, line, linelen);
1722
+ pp_handle_indent(pp, sb, indent, line, linelen);
1723
+ else
1724
+ strbuf_add(sb, line, linelen);
1725
strbuf_addch(sb, '\n');
1726
}
1727
}