Merge branch 'jk/empty-archive' into maint
[gitweb.git] / utf8.c
diff --git a/utf8.c b/utf8.c
index a4ee6650ef6c4c487c083aabf0a950b8ec317b09..8f6e84b7b3cf589f437a81ee81fd0228b2faf2f2 100644 (file)
--- a/utf8.c
+++ b/utf8.c
@@ -429,6 +429,27 @@ int same_encoding(const char *src, const char *dst)
        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.
@@ -486,9 +507,25 @@ char *reencode_string(const char *in, const char *out_encoding, const char *in_e
 
        if (!in_encoding)
                return NULL;
+
        conv = iconv_open(out_encoding, in_encoding);
-       if (conv == (iconv_t) -1)
-               return NULL;
+       if (conv == (iconv_t) -1) {
+               /*
+                * Some platforms do not have the variously spelled variants of
+                * UTF-8, so let's fall back to trying the most official
+                * spelling. We do so only as a fallback in case the platform
+                * does understand the user's spelling, but not our official
+                * one.
+                */
+               if (is_encoding_utf8(in_encoding))
+                       in_encoding = "UTF-8";
+               if (is_encoding_utf8(out_encoding))
+                       out_encoding = "UTF-8";
+               conv = iconv_open(out_encoding, in_encoding);
+               if (conv == (iconv_t) -1)
+                       return NULL;
+       }
+
        out = reencode_string_iconv(in, strlen(in), conv);
        iconv_close(conv);
        return out;