From: Junio C Hamano Date: Mon, 25 Feb 2013 16:03:59 +0000 (-0800) Subject: Merge branch 'jx/utf8-printf-width' into maint X-Git-Tag: v1.8.2-rc1~2^2~6 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/75288cc7e120cfcce4b11699d034bcadccdf9344?ds=inline;hp=-c Merge branch 'jx/utf8-printf-width' into maint * jx/utf8-printf-width: Add utf8_fprintf helper that returns correct number of columns --- 75288cc7e120cfcce4b11699d034bcadccdf9344 diff --combined utf8.c index a4ee6650ef,c7bda66e38..1087870c51 --- a/utf8.c +++ b/utf8.c @@@ -323,7 -323,7 +323,7 @@@ static size_t display_mode_esc_sequence * If indent is negative, assume that already -indent columns have been * consumed (and no extra indent is necessary for the first line). */ -int strbuf_add_wrapped_text(struct strbuf *buf, +void strbuf_add_wrapped_text(struct strbuf *buf, const char *text, int indent1, int indent2, int width) { int indent, w, assume_utf8 = 1; @@@ -332,7 -332,7 +332,7 @@@ if (width <= 0) { strbuf_add_indented_text(buf, text, indent1, indent2); - return 1; + return; } retry: @@@ -356,14 -356,14 +356,14 @@@ if (w <= width || !space) { const char *start = bol; if (!c && text == start) - return w; + return; if (space) start = space; else strbuf_addchars(buf, ' ', indent); strbuf_add(buf, start, text - start); if (!c) - return w; + return; space = text; if (c == '\t') w |= 0x07; @@@ -405,12 -405,13 +405,12 @@@ new_line } } -int strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len, +void strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len, int indent, int indent2, int width) { char *tmp = xstrndup(data, len); - int r = strbuf_add_wrapped_text(buf, tmp, indent, indent2, width); + strbuf_add_wrapped_text(buf, tmp, indent, indent2, width); free(tmp); - return r; } int is_encoding_utf8(const char *name) @@@ -429,6 -430,27 +429,27 @@@ int same_encoding(const char *src, cons return !strcasecmp(src, dst); } + /* + * Wrapper for fprintf and returns the total number of columns required + * for the printed string, assuming that the string is utf8. + */ + int utf8_fprintf(FILE *stream, const char *format, ...) + { + struct strbuf buf = STRBUF_INIT; + va_list arg; + int columns; + + va_start(arg, format); + strbuf_vaddf(&buf, format, arg); + va_end(arg); + + columns = fputs(buf.buf, stream); + if (0 <= columns) /* keep the error from the I/O */ + columns = utf8_strwidth(buf.buf); + strbuf_release(&buf); + return columns; + } + /* * Given a buffer and its encoding, return it re-encoded * with iconv. If the conversion fails, returns NULL. diff --combined utf8.h index a214238bdd,3ca85c7737..501b2bd9c4 --- a/utf8.h +++ b/utf8.h @@@ -8,10 -8,11 +8,11 @@@ int utf8_strwidth(const char *string) int is_utf8(const char *text); int is_encoding_utf8(const char *name); int same_encoding(const char *, const char *); + int utf8_fprintf(FILE *, const char *, ...); -int strbuf_add_wrapped_text(struct strbuf *buf, +void strbuf_add_wrapped_text(struct strbuf *buf, const char *text, int indent, int indent2, int width); -int strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len, +void strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len, int indent, int indent2, int width); #ifndef NO_ICONV