summaryrefslogtreecommitdiff
path: root/utf8.c
diff options
context:
space:
mode:
authorLars Schneider <larsxschneider@gmail.com>2018-04-15 18:16:04 (GMT)
committerJunio C Hamano <gitster@pobox.com>2018-04-16 02:40:56 (GMT)
commit2f0c4a362c5db677ee392a6a550632bfb22d8319 (patch)
treeae17d1999ec3d87d4f2f86337e6c5b6e59eb9ac6 /utf8.c
parent66b8af3e124a0c9dbad05f40c2f6d27614f34349 (diff)
downloadgit-2f0c4a362c5db677ee392a6a550632bfb22d8319.zip
git-2f0c4a362c5db677ee392a6a550632bfb22d8319.tar.gz
git-2f0c4a362c5db677ee392a6a550632bfb22d8319.tar.bz2
utf8: teach same_encoding() alternative UTF encoding names
The function same_encoding() could only recognize alternative names for UTF-8 encodings. Teach it to recognize all kinds of alternative UTF encoding names (e.g. utf16). While we are at it, fix a crash that would occur if same_encoding() was called with a NULL argument and a non-NULL argument. This function is used in a subsequent commit. Signed-off-by: Lars Schneider <larsxschneider@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'utf8.c')
-rw-r--r--utf8.c26
1 files changed, 24 insertions, 2 deletions
diff --git a/utf8.c b/utf8.c
index 2c27ce0..40f4b14 100644
--- a/utf8.c
+++ b/utf8.c
@@ -401,18 +401,40 @@ out:
strbuf_release(&sb_dst);
}
+/*
+ * Returns true (1) if the src encoding name matches the dst encoding
+ * name directly or one of its alternative names. E.g. UTF-16BE is the
+ * same as UTF16BE.
+ */
+static int same_utf_encoding(const char *src, const char *dst)
+{
+ if (istarts_with(src, "utf") && istarts_with(dst, "utf")) {
+ /* src[3] or dst[3] might be '\0' */
+ int i = (src[3] == '-' ? 4 : 3);
+ int j = (dst[3] == '-' ? 4 : 3);
+ return !strcasecmp(src+i, dst+j);
+ }
+ return 0;
+}
+
int is_encoding_utf8(const char *name)
{
if (!name)
return 1;
- if (!strcasecmp(name, "utf-8") || !strcasecmp(name, "utf8"))
+ if (same_utf_encoding("utf-8", name))
return 1;
return 0;
}
int same_encoding(const char *src, const char *dst)
{
- if (is_encoding_utf8(src) && is_encoding_utf8(dst))
+ static const char utf8[] = "UTF-8";
+
+ if (!src)
+ src = utf8;
+ if (!dst)
+ dst = utf8;
+ if (same_utf_encoding(src, dst))
return 1;
return !strcasecmp(src, dst);
}