Skip to content

Commit 0e18bcd

Browse files
gitsterpeff
authored andcommitted
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 <[email protected]>
1 parent 7e20105 commit 0e18bcd

File tree

6 files changed

+12
-4
lines changed

6 files changed

+12
-4
lines changed

builtin/mailinfo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ static void convert_to_utf8(struct strbuf *line, const char *charset)
507507
return;
508508
}
509509

510-
if (!strcasecmp(metainfo_charset, charset))
510+
if (same_encoding(metainfo_charset, charset))
511511
return;
512512
out = reencode_string(line->buf, metainfo_charset, charset);
513513
if (!out)

notes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@ void format_note(struct notes_tree *t, const unsigned char *object_sha1,
12211221
}
12221222

12231223
if (output_encoding && *output_encoding &&
1224-
strcmp(utf8, output_encoding)) {
1224+
!is_encoding_utf8(output_encoding)) {
12251225
char *reencoded = reencode_string(msg, output_encoding, utf8);
12261226
if (reencoded) {
12271227
free(msg);

pretty.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ char *logmsg_reencode(const struct commit *commit,
504504
return NULL;
505505
encoding = get_header(commit, "encoding");
506506
use_encoding = encoding ? encoding : utf8;
507-
if (!strcmp(use_encoding, output_encoding))
507+
if (same_encoding(use_encoding, output_encoding))
508508
if (encoding) /* we'll strip encoding header later */
509509
out = xstrdup(commit->buffer);
510510
else

sequencer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static int get_message(struct commit *commit, struct commit_message *out)
5858

5959
out->reencoded_message = NULL;
6060
out->message = commit->buffer;
61-
if (strcmp(encoding, git_commit_encoding))
61+
if (same_encoding(encoding, git_commit_encoding))
6262
out->reencoded_message = reencode_string(commit->buffer,
6363
git_commit_encoding, encoding);
6464
if (out->reencoded_message)

utf8.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,13 @@ int is_encoding_utf8(const char *name)
423423
return 0;
424424
}
425425

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+
426433
/*
427434
* Given a buffer and its encoding, return it re-encoded
428435
* with iconv. If the conversion fails, returns NULL.

utf8.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ int utf8_width(const char **start, size_t *remainder_p);
77
int utf8_strwidth(const char *string);
88
int is_utf8(const char *text);
99
int is_encoding_utf8(const char *name);
10+
int same_encoding(const char *, const char *);
1011

1112
int strbuf_add_wrapped_text(struct strbuf *buf,
1213
const char *text, int indent, int indent2, int width);

0 commit comments

Comments
 (0)