strbuf: use skip_prefix() in strbuf_addftime()

Use the now common skip_prefix() cascade instead of a case statement to parse the strftime(3) format in strbuf_addftime(). skip_prefix() parses the "fmt" pointer and advances it appropriately, making additional pointer arithmetic unnecessary. The resulting code is more compact and consistent with most other strbuf_expand_step() loops. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jul 16, 2023 at 10:52 UTC 945c72250afcf50a0f5394151b76d5da28fa6f94
1 file changed +6 -18
strbuf.c
+6 -18
@@ -996,31 +996,19 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
996 * of seconds.
997 */
998 while (strbuf_expand_step(&munged_fmt, &fmt)) {
999 - switch (*fmt) {
1000 - case '%':
999 + if (skip_prefix(fmt, "%", &fmt))
1000 strbuf_addstr(&munged_fmt, "%%");
1002 - fmt++;
1003 - break;
1004 - case 's':
1001 + else if (skip_prefix(fmt, "s", &fmt))
1002 strbuf_addf(&munged_fmt, "%"PRItime,
1003 (timestamp_t)tm_to_time_t(tm) -
1004 3600 * (tz_offset / 100) -
1005 60 * (tz_offset % 100));
1009 - fmt++;
1010 - break;
1011 - case 'z':
1006 + else if (skip_prefix(fmt, "z", &fmt))
1007 strbuf_addf(&munged_fmt, "%+05d", tz_offset);
1013 - fmt++;
1014 - break;
1015 - case 'Z':
1016 - if (suppress_tz_name) {
1017 - fmt++;
1018 - break;
1019 - }
1020 - /* FALLTHROUGH */
1021 - default:
1008 + else if (suppress_tz_name && skip_prefix(fmt, "Z", &fmt))
1009 + ; /* nothing */
1010 + else
1011 strbuf_addch(&munged_fmt, '%');
1023 - }
1012 }
1013 fmt = munged_fmt.buf;
1014