Skip to content

Commit ed28358

Browse files
rscharfegitster
authored andcommitted
convert: use skip_iprefix() in validate_encoding()
Use skip_iprefix() to parse "UTF" case-insensitively instead of checking with istarts_with(), building an upper-case version and then using skip_prefix() on it. This gets rid of duplicate code and of a small allocation. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 89f8cab commit ed28358

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

convert.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,12 @@ static int will_convert_lf_to_crlf(struct text_stat *stats,
270270
static int validate_encoding(const char *path, const char *enc,
271271
const char *data, size_t len, int die_on_error)
272272
{
273+
const char *stripped;
274+
273275
/* We only check for UTF here as UTF?? can be an alias for UTF-?? */
274-
if (istarts_with(enc, "UTF")) {
276+
if (skip_iprefix(enc, "UTF", &stripped)) {
277+
skip_prefix(stripped, "-", &stripped);
278+
275279
/*
276280
* Check for detectable errors in UTF encodings
277281
*/
@@ -285,15 +289,10 @@ static int validate_encoding(const char *path, const char *enc,
285289
*/
286290
const char *advise_msg = _(
287291
"The file '%s' contains a byte order "
288-
"mark (BOM). Please use UTF-%s as "
292+
"mark (BOM). Please use UTF-%.*s as "
289293
"working-tree-encoding.");
290-
const char *stripped = NULL;
291-
char *upper = xstrdup_toupper(enc);
292-
upper[strlen(upper)-2] = '\0';
293-
if (skip_prefix(upper, "UTF", &stripped))
294-
skip_prefix(stripped, "-", &stripped);
295-
advise(advise_msg, path, stripped);
296-
free(upper);
294+
int stripped_len = strlen(stripped) - strlen("BE");
295+
advise(advise_msg, path, stripped_len, stripped);
297296
if (die_on_error)
298297
die(error_msg, path, enc);
299298
else {
@@ -308,12 +307,7 @@ static int validate_encoding(const char *path, const char *enc,
308307
"mark (BOM). Please use UTF-%sBE or UTF-%sLE "
309308
"(depending on the byte order) as "
310309
"working-tree-encoding.");
311-
const char *stripped = NULL;
312-
char *upper = xstrdup_toupper(enc);
313-
if (skip_prefix(upper, "UTF", &stripped))
314-
skip_prefix(stripped, "-", &stripped);
315310
advise(advise_msg, path, stripped, stripped);
316-
free(upper);
317311
if (die_on_error)
318312
die(error_msg, path, enc);
319313
else {

0 commit comments

Comments
 (0)