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

[PATCH v2] cache_tree_find(): remove redundant checks

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Mar 4, 2014, 08:31 UTC
Message-ID
<1393921868-4382-1-git-send-email-mhagger@alum.mit.edu>

The beginning of the loop ensures that slash can never be NULL. So don't keep checking whether it is NULL later in the loop.

Furthermore, there is no need for an early
    return it;

from the loop if slash points at the end of the string, because that is exactly what will happen when the while condition fails at the start of the next iteration.

Helped-by: David Kastrup <dak@gnu.org>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
I incorporated David's suggestion, and then realized that yet another
check was superfluous, so I removed that one too.
 cache-tree.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
diff --git a/cache-tree.c b/cache-tree.c
index 0bbec43..19252c3 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -551,25 +551,22 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat
 	if (!it)
 		return NULL;
 	while (*path) {
-		const char *slash;
 		struct cache_tree_sub *sub;
+		const char *slash = strchr(path, '/');
 
-		slash = strchr(path, '/');
 		if (!slash)
 			slash = path + strlen(path);
-		/* between path and slash is the name of the
-		 * subtree to look for.
+		/*
+		 * Between path and slash is the name of the subtree
+		 * to look for.
 		 */
 		sub = find_subtree(it, path, slash - path, 0);
 		if (!sub)
 			return NULL;
 		it = sub->cache_tree;
-		if (slash)
-			while (*slash && *slash == '/')
-				slash++;
-		if (!slash || !*slash)
-			return it; /* prefix ended with slashes */
 		path = slash;
+		while (*path == '/')
+			path++;
 	}
 	return it;
 }
-- 
1.9.0
Next: David Kastrup
Message 1 of 11 in “cache_tree_find(): remove redundant checks”
  1. cache_tree_find(): remove redundant checksMichael Haggerty, Mar 4, 2014
  2. David KastrupMar 4, 2014
  3. Michael HaggertyMar 4, 2014
  4. David KastrupMar 4, 2014
  5. Junio C HamanoMar 4, 2014
  6. Michael HaggertyMar 4, 2014
  7. Junio C HamanoMar 4, 2014
  8. Junio C HamanoMar 5, 2014
  9. David KastrupMar 5, 2014
  10. Junio C HamanoMar 5, 2014
  11. [microproject idea]Junio C Hamano, Mar 4, 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.