From 0e18bcd5e937b9e5857892e7f5c80c3c5b1da5d4 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 18 Oct 2012 22:41:56 -0700 Subject: 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 --- builtin/mailinfo.c | 2 +- notes.c | 2 +- pretty.c | 2 +- sequencer.c | 2 +- utf8.c | 7 +++++++ utf8.h | 1 + 6 files changed, 12 insertions(+), 4 deletions(-) diff --git a/builtin/mailinfo.c b/builtin/mailinfo.c index fe128572f7..90b158d4f5 100644 --- a/builtin/mailinfo.c +++ b/builtin/mailinfo.c @@ -507,7 +507,7 @@ static void convert_to_utf8(struct strbuf *line, const char *charset) return; } - if (!strcasecmp(metainfo_charset, charset)) + if (same_encoding(metainfo_charset, charset)) return; out = reencode_string(line->buf, metainfo_charset, charset); if (!out) diff --git a/notes.c b/notes.c index 93e9868d5d..e48f6604d8 100644 --- a/notes.c +++ b/notes.c @@ -1221,7 +1221,7 @@ void format_note(struct notes_tree *t, const unsigned char *object_sha1, } if (output_encoding && *output_encoding && - strcmp(utf8, output_encoding)) { + !is_encoding_utf8(output_encoding)) { char *reencoded = reencode_string(msg, output_encoding, utf8); if (reencoded) { free(msg); diff --git a/pretty.c b/pretty.c index 8b1ea9ffad..e87fe9fec3 100644 --- a/pretty.c +++ b/pretty.c @@ -504,7 +504,7 @@ char *logmsg_reencode(const struct commit *commit, return NULL; encoding = get_header(commit, "encoding"); use_encoding = encoding ? encoding : utf8; - if (!strcmp(use_encoding, output_encoding)) + if (same_encoding(use_encoding, output_encoding)) if (encoding) /* we'll strip encoding header later */ out = xstrdup(commit->buffer); else diff --git a/sequencer.c b/sequencer.c index bd626806d6..f2f5b137ea 100644 --- a/sequencer.c +++ b/sequencer.c @@ -58,7 +58,7 @@ static int get_message(struct commit *commit, struct commit_message *out) out->reencoded_message = NULL; out->message = commit->buffer; - if (strcmp(encoding, git_commit_encoding)) + if (same_encoding(encoding, git_commit_encoding)) out->reencoded_message = reencode_string(commit->buffer, git_commit_encoding, encoding); if (out->reencoded_message) diff --git a/utf8.c b/utf8.c index a544f15456..6a52834576 100644 --- a/utf8.c +++ b/utf8.c @@ -423,6 +423,13 @@ int is_encoding_utf8(const char *name) return 0; } +int same_encoding(const char *src, const char *dst) +{ + if (is_encoding_utf8(src) && is_encoding_utf8(dst)) + return 1; + return !strcasecmp(src, dst); +} + /* * Given a buffer and its encoding, return it re-encoded * with iconv. If the conversion fails, returns NULL. diff --git a/utf8.h b/utf8.h index 3c0ae7624e..93ef60042c 100644 --- a/utf8.h +++ b/utf8.h @@ -7,6 +7,7 @@ int utf8_width(const char **start, size_t *remainder_p); int utf8_strwidth(const char *string); int is_utf8(const char *text); int is_encoding_utf8(const char *name); +int same_encoding(const char *, const char *); int strbuf_add_wrapped_text(struct strbuf *buf, const char *text, int indent, int indent2, int width); -- cgit v1.2.3