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

[PATCH 5/9] use strip_suffix instead of ends_with in simple cases

From
Jeff King <peff@peff.net>
Date
Jun 30, 2014, 16:58 UTC
Message-ID
<20140630165850.GE16637@sigill.intra.peff.net>
In-Reply-To
<20140630165526.GA15690@sigill.intra.peff.net>
When stripping a suffix like:
  if (ends_with(str, "foo"))
	buf = xmemdupz(str, strlen(str) - 3);

we can instead use strip_suffix to avoid the constant 3, which must match the literal "foo" (we sometimes use strlen("foo") instead, but that means we are repeating ourselves). The example above becomes:

  if (strip_suffix(str, "foo", &len))
	buf = xmemdupz(str, len);

This also saves a strlen(), since we calculate the string length when detecting the suffix.

Note that in some cases we also switch from xstrndup to xmemdupz, which saves a further strlen call.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/remote.c | 13 +++++++------
 builtin/repack.c |  5 ++---
 connected.c      |  6 +++---
 help.c           |  5 ++---
 4 files changed, 14 insertions(+), 15 deletions(-)
diff --git a/builtin/remote.c b/builtin/remote.c
index c9102e8..7c2999e 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -265,16 +265,17 @@ static int config_read_branches(const char *key, const char *value, void *cb)
 		struct string_list_item *item;
 		struct branch_info *info;
 		enum { REMOTE, MERGE, REBASE } type;
+		size_t key_len;
 
 		key += 7;
-		if (ends_with(key, ".remote")) {
-			name = xstrndup(key, strlen(key) - 7);
+		if (strip_suffix(key, ".remote", &key_len)) {
+			name = xmemdupz(key, key_len);
 			type = REMOTE;
-		} else if (ends_with(key, ".merge")) {
-			name = xstrndup(key, strlen(key) - 6);
+		} else if (strip_suffix(key, ".merge", &key_len)) {
+			name = xmemdupz(key, key_len);
 			type = MERGE;
-		} else if (ends_with(key, ".rebase")) {
-			name = xstrndup(key, strlen(key) - 7);
+		} else if (strip_suffix(key, ".rebase", &key_len)) {
+			name = xmemdupz(key, key_len);
 			type = REBASE;
 		} else
 			return 0;
diff --git a/builtin/repack.c b/builtin/repack.c
index ff2216a..a77e743 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -83,16 +83,15 @@ static void get_non_kept_pack_filenames(struct string_list *fname_list)
 	DIR *dir;
 	struct dirent *e;
 	char *fname;
-	size_t len;
 
 	if (!(dir = opendir(packdir)))
 		return;
 
 	while ((e = readdir(dir)) != NULL) {
-		if (!ends_with(e->d_name, ".pack"))
+		size_t len;
+		if (!strip_suffix(e->d_name, ".pack", &len))
 			continue;
 
-		len = strlen(e->d_name) - strlen(".pack");
 		fname = xmemdupz(e->d_name, len);
 
 		if (!file_exists(mkpath("%s/%s.keep", packdir, fname)))
diff --git a/connected.c b/connected.c
index be0253e..dae9c99 100644
--- a/connected.c
+++ b/connected.c
@@ -31,6 +31,7 @@ static int check_everything_connected_real(sha1_iterate_fn fn,
 	unsigned char sha1[20];
 	int err = 0, ac = 0;
 	struct packed_git *new_pack = NULL;
+	size_t base_len;
 
 	if (fn(cb_data, sha1))
 		return err;
@@ -38,10 +39,9 @@ static int check_everything_connected_real(sha1_iterate_fn fn,
 	if (transport && transport->smart_options &&
 	    transport->smart_options->self_contained_and_connected &&
 	    transport->pack_lockfile &&
-	    ends_with(transport->pack_lockfile, ".keep")) {
+	    strip_suffix(transport->pack_lockfile, ".keep", &base_len)) {
 		struct strbuf idx_file = STRBUF_INIT;
-		strbuf_addstr(&idx_file, transport->pack_lockfile);
-		strbuf_setlen(&idx_file, idx_file.len - 5); /* ".keep" */
+		strbuf_add(&idx_file, transport->pack_lockfile, base_len);
 		strbuf_addstr(&idx_file, ".idx");
 		new_pack = add_packed_git(idx_file.buf, idx_file.len, 1);
 		strbuf_release(&idx_file);
diff --git a/help.c b/help.c
index 372728f..97567c4 100644
--- a/help.c
+++ b/help.c
@@ -145,7 +145,7 @@ static void list_commands_in_dir(struct cmdnames *cmds,
 	len = buf.len;
 
 	while ((de = readdir(dir)) != NULL) {
-		int entlen;
+		size_t entlen;
 
 		if (!starts_with(de->d_name, prefix))
 			continue;
@@ -156,8 +156,7 @@ static void list_commands_in_dir(struct cmdnames *cmds,
 			continue;
 
 		entlen = strlen(de->d_name) - prefix_len;
-		if (ends_with(de->d_name, ".exe"))
-			entlen -= 4;
+		strip_suffix(de->d_name, ".exe", &entlen);
 
 		add_cmdname(cmds, de->d_name + prefix_len, entlen);
 	}
-- 
2.0.0.566.gfe3e6b2
Previous: Jeff KingNext: Jeff King
Message 21 of 27 in “sha1_file: replace PATH_MAX buffer wirh strbuf in prepare_packed_git_one()”
  1. 1/2 sha1_file: replace PATH_MAX buffer wirh strbuf in prepare_packed_git_one()René Scharfe, Jun 28, 2014
  2. 2/4 sha1_file: use strncmp for string comparisonRené Scharfe, Jun 28, 2014
  3. Duy NguyenJun 29, 2014
  4. 1/2 sha1_file: replace PATH_MAX buffer wirh strbuf in, prepare_packed_git_one()René Scharfe, Jun 29, 2014
  5. 2/2 sha1_file: use strncmp for string comparisonRené Scharfe, Jun 29, 2014
  6. Jeff KingJun 30, 2014
  7. Duy NguyenJun 30, 2014
  8. Jeff KingJun 30, 2014
  9. René ScharfeJun 30, 2014
  10. Jeff KingJun 30, 2014
  11. René ScharfeJun 30, 2014
  12. Jeff KingJun 30, 2014
  13. 0/9 add strip_suffix as an alternative to ends_withJeff King, Jun 30, 2014
  14. 1/9 sha1_file: replace PATH_MAX buffer with strbuf in prepare_packed_git_one()Jeff King, Jun 30, 2014
  15. 2/9 add strip_suffix functionJeff King, Jun 30, 2014
  16. Junio C HamanoJul 2, 2014
  17. Jeff KingJul 2, 2014
  18. Junio C HamanoJul 2, 2014
  19. 3/9 implement ends_with via strip_suffixJeff King, Jun 30, 2014
  20. 4/9 replace has_extension with ends_withJeff King, Jun 30, 2014
  21. 5/9 use strip_suffix instead of ends_with in simple casesJeff King, Jun 30, 2014
  22. 6/9 index-pack: use strip_suffix to avoid magic numbersJeff King, Jun 30, 2014
  23. 7/9 strbuf: implement strbuf_strip_suffixJeff King, Jun 30, 2014
  24. 8/9 verify-pack: use strbuf_strip_suffixJeff King, Jun 30, 2014
  25. 9/9 prepare_packed_git_one: refactor duplicate-pack checkJeff King, Jun 30, 2014
  26. Junio C HamanoJul 1, 2014
  27. Jeff KingJun 30, 2014

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.