Merge branch 'jx/utf8-printf-width' into maint
authorJunio C Hamano <gitster@pobox.com>
Mon, 25 Feb 2013 16:03:59 +0000 (08:03 -0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 25 Feb 2013 16:03:59 +0000 (08:03 -0800)
* jx/utf8-printf-width:
Add utf8_fprintf helper that returns correct number of columns

1  2 
utf8.c
utf8.h
diff --combined utf8.c
index a4ee6650ef6c4c487c083aabf0a950b8ec317b09,c7bda66e3800abfd49eea2d6fafd95a2403a6b90..1087870c51caff3dd86a852ec5e8bf5875f6d797
--- 1/utf8.c
--- 2/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;
  
        if (width <= 0) {
                strbuf_add_indented_text(buf, text, indent1, indent2);
 -              return 1;
 +              return;
        }
  
  retry:
                        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 a214238bdd70c6b4eb41edf6626c5b396489ab50,3ca85c7737bff0b3df449cc9dc32f32230b522e1..501b2bd9c4fd3fdce6dcbba1a3e3f7c0211d6c8f
--- 1/utf8.h
--- 2/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