{"thread":{"id":"36066","subject":"[PATCH v3 1/6] cache_tree_find(): remove redundant checks","startedAt":"2014-03-05T17:26:24Z","lastAt":"2014-03-05T17:26:30Z","messageCount":7,"participants":["Michael Haggerty"],"isPatch":true,"patchVersion":3,"patchTotal":6},"messages":[{"id":"236099","messageId":"1394040390-7954-1-git-send-email-mhagger@alum.mit.edu","threadId":"36066","inReplyTo":null,"subject":"[PATCH v3 0/6] cache_tree_find() workover","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-03-05T17:26:24Z","receivedAt":"2014-03-05T17:26:24Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Who would have expected so much slop in one little function?  Here are\nthe changes coming out of a long email thread.\n\nI feel a little bit silly submitting six separate patches, but they\nreally are independent.  And two of the changes were suggested by\nother people, so splitting those out, at least, helps give credit\nwhere it is due.  But feel free to squash the patches together if you\nthink it is best.\n\nChanges since v2 (aside from the atomization):\n\n* Use strchrnul() to initialize slash (as suggested by Junio)\n\n* Don't initialize variable at declaration (as per Junio's\n  preference).\n\nThanks to Junio and David for this workout.\n\nMichael Haggerty (6):\n  cache_tree_find(): remove redundant checks\n  cache_tree_find(): initialize slash using strchrnul()\n  cache_tree_find(): fix comment formatting\n  cache_tree_find(): remove redundant check\n  cache_tree_find(): remove early return\n  cache_tree_find(): use path variable when passing over slashes\n\n cache-tree.c | 17 +++++++----------\n 1 file changed, 7 insertions(+), 10 deletions(-)\n\n-- \n1.9.0\n"},{"id":"236094","messageId":"1394040390-7954-2-git-send-email-mhagger@alum.mit.edu","threadId":"36066","inReplyTo":"1394040390-7954-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v3 1/6] cache_tree_find(): remove redundant checks","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-03-05T17:26:25Z","receivedAt":"2014-03-05T17:26:25Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"slash is initialized to a value that cannot be NULL.  So remove the\nguards against slash == NULL later in the loop.\n\nSuggested-by: David Kastrup <dak@gnu.org>\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n cache-tree.c | 7 +++----\n 1 file changed, 3 insertions(+), 4 deletions(-)\n\ndiff --git a/cache-tree.c b/cache-tree.c\nindex 0bbec43..4d439bd 100644\n--- a/cache-tree.c\n+++ b/cache-tree.c\n@@ -564,10 +564,9 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat\n \t\tif (!sub)\n \t\t\treturn NULL;\n \t\tit = sub->cache_tree;\n-\t\tif (slash)\n-\t\t\twhile (*slash && *slash == '/')\n-\t\t\t\tslash++;\n-\t\tif (!slash || !*slash)\n+\t\twhile (*slash && *slash == '/')\n+\t\t\tslash++;\n+\t\tif (!*slash)\n \t\t\treturn it; /* prefix ended with slashes */\n \t\tpath = slash;\n \t}\n-- \n1.9.0\n"},{"id":"236095","messageId":"1394040390-7954-3-git-send-email-mhagger@alum.mit.edu","threadId":"36066","inReplyTo":"1394040390-7954-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v3 2/6] cache_tree_find(): initialize slash using strchrnul()","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-03-05T17:26:26Z","receivedAt":"2014-03-05T17:26:26Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Suggested-by: Junio Hamano <gitster@pobox.com>\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n cache-tree.c | 4 +---\n 1 file changed, 1 insertion(+), 3 deletions(-)\n\ndiff --git a/cache-tree.c b/cache-tree.c\nindex 4d439bd..d00f4ef 100644\n--- a/cache-tree.c\n+++ b/cache-tree.c\n@@ -554,9 +554,7 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat\n \t\tconst char *slash;\n \t\tstruct cache_tree_sub *sub;\n \n-\t\tslash = strchr(path, '/');\n-\t\tif (!slash)\n-\t\t\tslash = path + strlen(path);\n+\t\tslash = strchrnul(path, '/');\n \t\t/* between path and slash is the name of the\n \t\t * subtree to look for.\n \t\t */\n-- \n1.9.0\n"},{"id":"236100","messageId":"1394040390-7954-4-git-send-email-mhagger@alum.mit.edu","threadId":"36066","inReplyTo":"1394040390-7954-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v3 3/6] cache_tree_find(): fix comment formatting","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-03-05T17:26:27Z","receivedAt":"2014-03-05T17:26:27Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n cache-tree.c | 5 +++--\n 1 file changed, 3 insertions(+), 2 deletions(-)\n\ndiff --git a/cache-tree.c b/cache-tree.c\nindex d00f4ef..408ee57 100644\n--- a/cache-tree.c\n+++ b/cache-tree.c\n@@ -555,8 +555,9 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat\n \t\tstruct cache_tree_sub *sub;\n \n \t\tslash = strchrnul(path, '/');\n-\t\t/* between path and slash is the name of the\n-\t\t * subtree to look for.\n+\t\t/*\n+\t\t * Between path and slash is the name of the subtree\n+\t\t * to look for.\n \t\t */\n \t\tsub = find_subtree(it, path, slash - path, 0);\n \t\tif (!sub)\n-- \n1.9.0\n"},{"id":"236097","messageId":"1394040390-7954-5-git-send-email-mhagger@alum.mit.edu","threadId":"36066","inReplyTo":"1394040390-7954-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v3 4/6] cache_tree_find(): remove redundant check","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-03-05T17:26:28Z","receivedAt":"2014-03-05T17:26:28Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"If *slash == '/', then it is necessarily non-NUL.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n cache-tree.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/cache-tree.c b/cache-tree.c\nindex 408ee57..39ad8c9 100644\n--- a/cache-tree.c\n+++ b/cache-tree.c\n@@ -563,7 +563,7 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat\n \t\tif (!sub)\n \t\t\treturn NULL;\n \t\tit = sub->cache_tree;\n-\t\twhile (*slash && *slash == '/')\n+\t\twhile (*slash == '/')\n \t\t\tslash++;\n \t\tif (!*slash)\n \t\t\treturn it; /* prefix ended with slashes */\n-- \n1.9.0\n"},{"id":"236096","messageId":"1394040390-7954-6-git-send-email-mhagger@alum.mit.edu","threadId":"36066","inReplyTo":"1394040390-7954-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v3 5/6] cache_tree_find(): remove early return","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-03-05T17:26:29Z","receivedAt":"2014-03-05T17:26:29Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"There is no need for an early\n\n    return it;\n\nfrom the loop if slash points at the end of the string, because that\nis exactly what will happen when the while condition fails at the\nstart of the next iteration.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n cache-tree.c | 2 --\n 1 file changed, 2 deletions(-)\n\ndiff --git a/cache-tree.c b/cache-tree.c\nindex 39ad8c9..17db9f9 100644\n--- a/cache-tree.c\n+++ b/cache-tree.c\n@@ -565,8 +565,6 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat\n \t\tit = sub->cache_tree;\n \t\twhile (*slash == '/')\n \t\t\tslash++;\n-\t\tif (!*slash)\n-\t\t\treturn it; /* prefix ended with slashes */\n \t\tpath = slash;\n \t}\n \treturn it;\n-- \n1.9.0\n"},{"id":"236098","messageId":"1394040390-7954-7-git-send-email-mhagger@alum.mit.edu","threadId":"36066","inReplyTo":"1394040390-7954-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v3 6/6] cache_tree_find(): use path variable when passing over slashes","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-03-05T17:26:30Z","receivedAt":"2014-03-05T17:26:30Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"The search for the end of the slashes is part of the update of the\npath variable for the next iteration as opposed to an update of the\nslash variable.  So iterate using path rather than slash.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n cache-tree.c | 5 +++--\n 1 file changed, 3 insertions(+), 2 deletions(-)\n\ndiff --git a/cache-tree.c b/cache-tree.c\nindex 17db9f9..7f8d74d 100644\n--- a/cache-tree.c\n+++ b/cache-tree.c\n@@ -563,9 +563,10 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat\n \t\tif (!sub)\n \t\t\treturn NULL;\n \t\tit = sub->cache_tree;\n-\t\twhile (*slash == '/')\n-\t\t\tslash++;\n+\n \t\tpath = slash;\n+\t\twhile (*path == '/')\n+\t\t\tpath++;\n \t}\n \treturn it;\n }\n-- \n1.9.0\n"}]}