reencode_string(): introduce and use same_encoding()

Callers of reencode_string() that re-encodes a string from one encoding to another all used ad-hoc way to bypass the case where the input and the output encodings are the same. Some did strcmp(), some did strcasecmp(), yet some others when converting to UTF-8 used is_encoding_utf8(). Introduce same_encoding() helper function to make these callers use the same logic. Notably, is_encoding_utf8() has a work-around for common misconfiguration to use "utf8" to name UTF-8 encoding, which does not match "UTF-8" hence strcasecmp() would not consider the same. Make use of it in this helper function. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 18, 2012 at 22:41 UTC 0e18bcd5e937b9e5857892e7f5c80c3c5b1da5d4
6 files changed +12 -4
builtin/mailinfo.c
+1 -1
@@ -507,7 +507,7 @@ static void convert_to_utf8(struct strbuf *line, const char *charset)
507 return;
508 }
509
510 - if (!strcasecmp(metainfo_charset, charset))
510 + if (same_encoding(metainfo_charset, charset))
511 return;
512 out = reencode_string(line->buf, metainfo_charset, charset);
513 if (!out)
notes.c
+1 -1
@@ -1221,7 +1221,7 @@ void format_note(struct notes_tree *t, const unsigned char *object_sha1,
1221 }
1222
1223 if (output_encoding && *output_encoding &&
1224 - strcmp(utf8, output_encoding)) {
1224 + !is_encoding_utf8(output_encoding)) {
1225 char *reencoded = reencode_string(msg, output_encoding, utf8);
1226 if (reencoded) {
1227 free(msg);
pretty.c
+1 -1
@@ -504,7 +504,7 @@ char *logmsg_reencode(const struct commit *commit,
504 return NULL;
505 encoding = get_header(commit, "encoding");
506 use_encoding = encoding ? encoding : utf8;
507 - if (!strcmp(use_encoding, output_encoding))
507 + if (same_encoding(use_encoding, output_encoding))
508 if (encoding) /* we'll strip encoding header later */
509 out = xstrdup(commit->buffer);
510 else
sequencer.c
+1 -1
@@ -58,7 +58,7 @@ static int get_message(struct commit *commit, struct commit_message *out)
58
59 out->reencoded_message = NULL;
60 out->message = commit->buffer;
61 - if (strcmp(encoding, git_commit_encoding))
61 + if (same_encoding(encoding, git_commit_encoding))
62 out->reencoded_message = reencode_string(commit->buffer,
63 git_commit_encoding, encoding);
64 if (out->reencoded_message)
utf8.c
+7
@@ -423,6 +423,13 @@ int is_encoding_utf8(const char *name)
423 return 0;
424 }
425
426 +int same_encoding(const char *src, const char *dst)
427 +{
428 + if (is_encoding_utf8(src) && is_encoding_utf8(dst))
429 + return 1;
430 + return !strcasecmp(src, dst);
431 +}
432 +
433 /*
434 * Given a buffer and its encoding, return it re-encoded
435 * with iconv. If the conversion fails, returns NULL.
utf8.h
+1
@@ -7,6 +7,7 @@ int utf8_width(const char **start, size_t *remainder_p);
7 int utf8_strwidth(const char *string);
8 int is_utf8(const char *text);
9 int is_encoding_utf8(const char *name);
10 +int same_encoding(const char *, const char *);
11
12 int strbuf_add_wrapped_text(struct strbuf *buf,
13 const char *text, int indent, int indent2, int width);