git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] strbuf: use skip_prefix() in strbuf_addftime()

From
René Scharfe <l.s.r@web.de>
Date
Jul 16, 2023, 08:52 UTC
Message-ID
<fccba24c-584d-6329-69a7-75cf0458af7d@web.de>

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>
---
 strbuf.c | 24 ++++++------------------
 1 file changed, 6 insertions(+), 18 deletions(-)
diff --git a/strbuf.c b/strbuf.c
index 399242684b..837975ef9c 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -935,31 +935,19 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
 	 * of seconds.
 	 */
 	while (strbuf_expand_step(&munged_fmt, &fmt)) {
-		switch (*fmt) {
-		case '%':
+		if (skip_prefix(fmt, "%", &fmt))
 			strbuf_addstr(&munged_fmt, "%%");
-			fmt++;
-			break;
-		case 's':
+		else if (skip_prefix(fmt, "s", &fmt))
 			strbuf_addf(&munged_fmt, "%"PRItime,
 				    (timestamp_t)tm_to_time_t(tm) -
 				    3600 * (tz_offset / 100) -
 				    60 * (tz_offset % 100));
-			fmt++;
-			break;
-		case 'z':
+		else if (skip_prefix(fmt, "z", &fmt))
 			strbuf_addf(&munged_fmt, "%+05d", tz_offset);
-			fmt++;
-			break;
-		case 'Z':
-			if (suppress_tz_name) {
-				fmt++;
-				break;
-			}
-			/* FALLTHROUGH */
-		default:
+		else if (suppress_tz_name && skip_prefix(fmt, "Z", &fmt))
+			; /* nothing */
+		else
 			strbuf_addch(&munged_fmt, '%');
-		}
 	}
 	fmt = munged_fmt.buf;

--
2.41.0
Message 1 of 1 in “strbuf: use skip_prefix() in strbuf_addftime()”
  1. strbuf: use skip_prefix() in strbuf_addftime()René Scharfe, Jul 16, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.