@matheusbsbs / matheus-bagliano / commits / ed28358833
Fork 0

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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Nov 8, 2019 at 21:27 UTC ed283588330968dfefba37032980dfe2a8df63e4
1 file changed +8 -14
convert.c
+8 -14
@@ -270,8 +270,12 @@ static int will_convert_lf_to_crlf(struct text_stat *stats,
270 static int validate_encoding(const char *path, const char *enc,
271 const char *data, size_t len, int die_on_error)
272 {
273 + const char *stripped;
274 +
275 /* 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 +
279 /*
280 * Check for detectable errors in UTF encodings
281 */
@@ -285,15 +289,10 @@ static int validate_encoding(const char *path, const char *enc,
289 */
290 const char *advise_msg = _(
291 "The file '%s' contains a byte order "
288 - "mark (BOM). Please use UTF-%s as "
292 + "mark (BOM). Please use UTF-%.*s as "
293 "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);
296 if (die_on_error)
297 die(error_msg, path, enc);
298 else {
@@ -308,12 +307,7 @@ static int validate_encoding(const char *path, const char *enc,
307 "mark (BOM). Please use UTF-%sBE or UTF-%sLE "
308 "(depending on the byte order) as "
309 "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);
310 advise(advise_msg, path, stripped, stripped);
316 - free(upper);
311 if (die_on_error)
312 die(error_msg, path, enc);
313 else {