From: Jacob Keller <jacob.keller@xxxxxxxxx> In commit 7cc13c717b52 ("pretty: expand tabs in indented logs to make things line up properly", 2016-03-16) a new function was added to insert a line into a strbuf while expanding the tabs into spaces. This functionality was used to help show the log message correctly when it has been indented, so as to properly display the expected output. This functionality will be useful in a future patch that adds similar functionality into git diff, so lets move it into strbuf.c and make it a public function. While we're doing this, rename a few of the variables to fix the surrounding strbuf code. Signed-off-by: Jacob Keller <jacob.keller@xxxxxxxxx> --- pretty.c | 50 -------------------------------------------------- strbuf.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ strbuf.h | 6 ++++++ 3 files changed, 56 insertions(+), 50 deletions(-) diff --git a/pretty.c b/pretty.c index d0f86f5d85ca..70368509ffea 100644 --- a/pretty.c +++ b/pretty.c @@ -1658,56 +1658,6 @@ void pp_title_line(struct pretty_print_context *pp, strbuf_release(&title); } -static int pp_utf8_width(const char *start, const char *end) -{ - int width = 0; - size_t remain = end - start; - - while (remain) { - int n = utf8_width(&start, &remain); - if (n < 0 || !start) - return -1; - width += n; - } - return width; -} - -static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth, - const char *line, int linelen) -{ - const char *tab; - - while ((tab = memchr(line, '\t', linelen)) != NULL) { - int width = pp_utf8_width(line, tab); - - /* - * If it wasn't well-formed utf8, or it - * had characters with badly defined - * width (control characters etc), just - * give up on trying to align things. - */ - if (width < 0) - break; - - /* Output the data .. */ - strbuf_add(sb, line, tab - line); - - /* .. and the de-tabified tab */ - strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth)); - - /* Skip over the printed part .. */ - linelen -= tab + 1 - line; - line = tab + 1; - } - - /* - * Print out everything after the last tab without - * worrying about width - there's nothing more to - * align. - */ - strbuf_add(sb, line, linelen); -} - /* * pp_handle_indent() prints out the intendation, and * the whole line (without the final newline), after diff --git a/strbuf.c b/strbuf.c index 00457940cfc1..6cecfcadb05b 100644 --- a/strbuf.c +++ b/strbuf.c @@ -275,6 +275,56 @@ void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...) strbuf_release(&buf); } +static int find_utf8_width(const char *start, const char *end) +{ + int width = 0; + size_t remain = end - start; + + while (remain) { + int n = utf8_width(&start, &remain); + if (n < 0 || !start) + return -1; + width += n; + } + return width; +} + +void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth, + const char *buf, size_t size) +{ + const char *tab; + + while ((tab = memchr(buf, '\t', size)) != NULL) { + int width = find_utf8_width(buf, tab); + + /* + * If it wasn't well-formed utf8, or it + * had characters with badly defined + * width (control characters etc), just + * give up on trying to align things. + */ + if (width < 0) + break; + + /* Output the data .. */ + strbuf_add(sb, buf, tab - buf); + + /* .. and the de-tabified tab */ + strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth)); + + /* Skip over the printed part .. */ + size -= tab + 1 - buf; + buf = tab + 1; + } + + /* + * Print out everything after the last tab without + * worrying about width - there's nothing more to + * align. + */ + strbuf_add(sb, buf, size); +} + void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap) { int len; diff --git a/strbuf.h b/strbuf.h index 80047b1bb7b8..17e04911833e 100644 --- a/strbuf.h +++ b/strbuf.h @@ -238,6 +238,12 @@ extern void strbuf_splice(struct strbuf *, size_t pos, size_t len, */ extern void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size); +/** + * Add a NUL-terminated string to the buffer. Tabs will be expanded using the + * provided tabwidth. + */ +extern void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth, + const char *buf, size_t size); /** * Add data of given length to the buffer. -- 2.12.2.650.ga248b8c51283