Imported Upstream version 2.15.3
[platform/upstream/git.git] / utf8.c
diff --git a/utf8.c b/utf8.c
index 520fbb4..f04c244 100644 (file)
--- a/utf8.c
+++ b/utf8.c
@@ -32,7 +32,7 @@ static int bisearch(ucs_char_t ucs, const struct interval *table, int max)
        if (ucs < table[0].first || ucs > table[max].last)
                return 0;
        while (max >= min) {
-               mid = (min + max) / 2;
+               mid = min + (max - min) / 2;
                if (ucs > table[mid].last)
                        min = mid + 1;
                else if (ucs < table[mid].first)
@@ -381,7 +381,7 @@ void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,
                old = src;
                n = utf8_width((const char**)&src, NULL);
                if (!src)       /* broken utf-8, do nothing */
-                       return;
+                       goto out;
                if (n && w >= pos && w < pos + width) {
                        if (subst) {
                                memcpy(dst, subst, subst_len);
@@ -397,6 +397,7 @@ void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,
        }
        strbuf_setlen(&sb_dst, dst - sb_dst.buf);
        strbuf_swap(sb_src, &sb_dst);
+out:
        strbuf_release(&sb_dst);
 }
 
@@ -489,6 +490,28 @@ char *reencode_string_iconv(const char *in, size_t insz, iconv_t conv, int *outs
        return out;
 }
 
+static const char *fallback_encoding(const char *name)
+{
+       /*
+        * 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(name))
+               return "UTF-8";
+
+       /*
+        * Even though latin-1 is still seen in e-mail
+        * headers, some platforms only install ISO-8859-1.
+        */
+       if (!strcasecmp(name, "latin-1"))
+               return "ISO-8859-1";
+
+       return name;
+}
+
 char *reencode_string_len(const char *in, int insz,
                          const char *out_encoding, const char *in_encoding,
                          int *outsz)
@@ -501,17 +524,9 @@ char *reencode_string_len(const char *in, int insz,
 
        conv = iconv_open(out_encoding, in_encoding);
        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";
+               in_encoding = fallback_encoding(in_encoding);
+               out_encoding = fallback_encoding(out_encoding);
+
                conv = iconv_open(out_encoding, in_encoding);
                if (conv == (iconv_t) -1)
                        return NULL;
@@ -605,31 +620,97 @@ static ucs_char_t next_hfs_char(const char **in)
        }
 }
 
-int is_hfs_dotgit(const char *path)
+static int is_hfs_dot_generic(const char *path,
+                             const char *needle, size_t needle_len)
 {
        ucs_char_t c;
 
        c = next_hfs_char(&path);
        if (c != '.')
                return 0;
-       c = next_hfs_char(&path);
 
        /*
         * there's a great deal of other case-folding that occurs
-        * in HFS+, but this is enough to catch anything that will
-        * convert to ".git"
+        * in HFS+, but this is enough to catch our fairly vanilla
+        * hard-coded needles.
         */
-       if (c != 'g' && c != 'G')
-               return 0;
-       c = next_hfs_char(&path);
-       if (c != 'i' && c != 'I')
-               return 0;
-       c = next_hfs_char(&path);
-       if (c != 't' && c != 'T')
-               return 0;
+       for (; needle_len > 0; needle++, needle_len--) {
+               c = next_hfs_char(&path);
+
+               /*
+                * We know our needles contain only ASCII, so we clamp here to
+                * make the results of tolower() sane.
+                */
+               if (c > 127)
+                       return 0;
+               if (tolower(c) != *needle)
+                       return 0;
+       }
+
        c = next_hfs_char(&path);
        if (c && !is_dir_sep(c))
                return 0;
 
        return 1;
 }
+
+/*
+ * Inline wrapper to make sure the compiler resolves strlen() on literals at
+ * compile time.
+ */
+static inline int is_hfs_dot_str(const char *path, const char *needle)
+{
+       return is_hfs_dot_generic(path, needle, strlen(needle));
+}
+
+int is_hfs_dotgit(const char *path)
+{
+       return is_hfs_dot_str(path, "git");
+}
+
+int is_hfs_dotgitmodules(const char *path)
+{
+       return is_hfs_dot_str(path, "gitmodules");
+}
+
+int is_hfs_dotgitignore(const char *path)
+{
+       return is_hfs_dot_str(path, "gitignore");
+}
+
+int is_hfs_dotgitattributes(const char *path)
+{
+       return is_hfs_dot_str(path, "gitattributes");
+}
+
+const char utf8_bom[] = "\357\273\277";
+
+int skip_utf8_bom(char **text, size_t len)
+{
+       if (len < strlen(utf8_bom) ||
+           memcmp(*text, utf8_bom, strlen(utf8_bom)))
+               return 0;
+       *text += strlen(utf8_bom);
+       return 1;
+}
+
+void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,
+                      const char *s)
+{
+       int slen = strlen(s);
+       int display_len = utf8_strnwidth(s, slen, 0);
+       int utf8_compensation = slen - display_len;
+
+       if (display_len >= width) {
+               strbuf_addstr(buf, s);
+               return;
+       }
+
+       if (position == ALIGN_LEFT)
+               strbuf_addf(buf, "%-*s", width + utf8_compensation, s);
+       else if (position == ALIGN_MIDDLE) {
+               int left = (width - display_len) / 2;
+               strbuf_addf(buf, "%*s%-*s", left, "", width - left + utf8_compensation, s);
+       } else if (position == ALIGN_RIGHT)
+               strbuf_addf(buf, "%*s", width + utf8_compensation, s);
+}