git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: lars.schneider@autodesk.com
To: git@vger.kernel.org
Cc: gitster@pobox.com, tboegi@web.de, j6t@kdbg.org,
	sunshine@sunshineco.com, peff@peff.net,
	ramsay@ramsayjones.plus.com, Johannes.Schindelin@gmx.de,
	pclouds@gmail.com, avarab@gmail.com,
	Lars Schneider <larsxschneider@gmail.com>
Subject: [PATCH v13 04/10] utf8: teach same_encoding() alternative UTF encoding names
Date: Sun, 15 Apr 2018 20:16:04 +0200	[thread overview]
Message-ID: <20180415181610.1612-5-lars.schneider@autodesk.com> (raw)
In-Reply-To: <20180415181610.1612-1-lars.schneider@autodesk.com>

From: Lars Schneider <larsxschneider@gmail.com>

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>
---
 utf8.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/utf8.c b/utf8.c
index 2c27ce0137..40f4b142d6 100644
--- a/utf8.c
+++ b/utf8.c
@@ -401,18 +401,40 @@ void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,
 	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);
 }
-- 
2.16.2


  parent reply	other threads:[~2018-04-15 18:16 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-15 18:16 [PATCH v13 00/10] convert: add support for different encodings lars.schneider
2018-04-15 18:16 ` [PATCH v13 01/10] strbuf: remove unnecessary NUL assignment in xstrdup_tolower() lars.schneider
2018-04-15 18:16 ` [PATCH v13 02/10] strbuf: add xstrdup_toupper() lars.schneider
2018-04-15 18:16 ` [PATCH v13 03/10] strbuf: add a case insensitive starts_with() lars.schneider
2018-04-15 18:16 ` lars.schneider [this message]
2018-04-15 18:16 ` [PATCH v13 05/10] utf8: add function to detect prohibited UTF-16/32 BOM lars.schneider
2018-04-15 18:16 ` [PATCH v13 06/10] utf8: add function to detect a missing " lars.schneider
2018-04-15 18:16 ` [PATCH v13 07/10] convert: add 'working-tree-encoding' attribute lars.schneider
2018-04-15 18:16 ` [PATCH v13 08/10] convert: check for detectable errors in UTF encodings lars.schneider
2018-04-15 18:16 ` [PATCH v13 09/10] convert: add tracing for 'working-tree-encoding' attribute lars.schneider
2018-04-15 18:16 ` [PATCH v13 10/10] convert: add round trip check based on 'core.checkRoundtripEncoding' lars.schneider

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180415181610.1612-5-lars.schneider@autodesk.com \
    --to=lars.schneider@autodesk.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=j6t@kdbg.org \
    --cc=larsxschneider@gmail.com \
    --cc=pclouds@gmail.com \
    --cc=peff@peff.net \
    --cc=ramsay@ramsayjones.plus.com \
    --cc=sunshine@sunshineco.com \
    --cc=tboegi@web.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).