Thread (1 message) 1 message, 1 author, 2016-06-15

Re: [PATCH] use strchrnul() in place of strchr() and strlen()

From: Junio C Hamano <hidden>
Date: 2016-06-15 23:00:13

Rohit Mani [off-list ref] writes:
Avoid scanning strings twice, once with strchr() and then with
strlen(), by using strchrnul(). Update the conditional expressions
involving the return value of strchrnul() with a check for '\0'.

Signed-off-by: Rohit Mani <redacted>
---
Nicely done.  I am not sure if you need to say the "update the
conditional...", which is a logical consequence of such a conversion
and goes without saying, though.
 cache-tree.c     |   16 +++++++---------
This part may overlap with other topics in flight, but I expect the
conflict resolution would be trivial.
quoted hunk
diff --git a/cache-tree.c b/cache-tree.c
index 0bbec43..21a13cf 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -121,11 +121,11 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
 
 	if (!it)
 		return;
-	slash = strchr(path, '/');
+	slash = strchrnul(path, '/');
 	it->entry_count = -1;
-	if (!slash) {
+	if (*slash == '\0') {
Let's just say

	if (!*slash)

instead; it is more idiomatic (I won't repeat this for other hunks).
 		int pos;
-		namelen = strlen(path);
+		namelen = slash - path;
After this "if (!*slash)", we compute "namelen = slash-path".
Perhaps we can lose this assignment and the other one by hoisting it
up before "if (!*slash)"?
quoted hunk
@@ -564,10 +562,10 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat
+		if (*slash == '\0' || !*slash)
Huh?  "The byte pointed at by 'slash' is NUL, or it is NUL"???

Other than that, looks good to me.

Thanks.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help