use strchrnul() in place of strchr() and strlen()
Avoid scanning strings twice, once with strchr() and then with strlen(), by using strchrnul(). Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Rohit Mani <rohit.mani@outlook.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
5f95c9f850
commit
2c5495f7b6
@ -1485,14 +1485,11 @@ static int tree_content_set(
|
||||
unsigned int i, n;
|
||||
struct tree_entry *e;
|
||||
|
||||
slash1 = strchr(p, '/');
|
||||
if (slash1)
|
||||
n = slash1 - p;
|
||||
else
|
||||
n = strlen(p);
|
||||
slash1 = strchrnul(p, '/');
|
||||
n = slash1 - p;
|
||||
if (!n)
|
||||
die("Empty path component found in input");
|
||||
if (!slash1 && !S_ISDIR(mode) && subtree)
|
||||
if (!*slash1 && !S_ISDIR(mode) && subtree)
|
||||
die("Non-directories cannot have subtrees");
|
||||
|
||||
if (!root->tree)
|
||||
@ -1501,7 +1498,7 @@ static int tree_content_set(
|
||||
for (i = 0; i < t->entry_count; i++) {
|
||||
e = t->entries[i];
|
||||
if (e->name->str_len == n && !strncmp_icase(p, e->name->str_dat, n)) {
|
||||
if (!slash1) {
|
||||
if (!*slash1) {
|
||||
if (!S_ISDIR(mode)
|
||||
&& e->versions[1].mode == mode
|
||||
&& !hashcmp(e->versions[1].sha1, sha1))
|
||||
@ -1552,7 +1549,7 @@ static int tree_content_set(
|
||||
e->versions[0].mode = 0;
|
||||
hashclr(e->versions[0].sha1);
|
||||
t->entries[t->entry_count++] = e;
|
||||
if (slash1) {
|
||||
if (*slash1) {
|
||||
e->tree = new_tree_content(8);
|
||||
e->versions[1].mode = S_IFDIR;
|
||||
tree_content_set(e, slash1 + 1, sha1, mode, subtree);
|
||||
@ -1576,11 +1573,8 @@ static int tree_content_remove(
|
||||
unsigned int i, n;
|
||||
struct tree_entry *e;
|
||||
|
||||
slash1 = strchr(p, '/');
|
||||
if (slash1)
|
||||
n = slash1 - p;
|
||||
else
|
||||
n = strlen(p);
|
||||
slash1 = strchrnul(p, '/');
|
||||
n = slash1 - p;
|
||||
|
||||
if (!root->tree)
|
||||
load_tree(root);
|
||||
@ -1594,7 +1588,7 @@ static int tree_content_remove(
|
||||
for (i = 0; i < t->entry_count; i++) {
|
||||
e = t->entries[i];
|
||||
if (e->name->str_len == n && !strncmp_icase(p, e->name->str_dat, n)) {
|
||||
if (slash1 && !S_ISDIR(e->versions[1].mode))
|
||||
if (*slash1 && !S_ISDIR(e->versions[1].mode))
|
||||
/*
|
||||
* If p names a file in some subdirectory, and a
|
||||
* file or symlink matching the name of the
|
||||
@ -1602,7 +1596,7 @@ static int tree_content_remove(
|
||||
* exist and need not be deleted.
|
||||
*/
|
||||
return 1;
|
||||
if (!slash1 || !S_ISDIR(e->versions[1].mode))
|
||||
if (!*slash1 || !S_ISDIR(e->versions[1].mode))
|
||||
goto del_entry;
|
||||
if (!e->tree)
|
||||
load_tree(e);
|
||||
@ -1644,11 +1638,8 @@ static int tree_content_get(
|
||||
unsigned int i, n;
|
||||
struct tree_entry *e;
|
||||
|
||||
slash1 = strchr(p, '/');
|
||||
if (slash1)
|
||||
n = slash1 - p;
|
||||
else
|
||||
n = strlen(p);
|
||||
slash1 = strchrnul(p, '/');
|
||||
n = slash1 - p;
|
||||
if (!n && !allow_root)
|
||||
die("Empty path component found in input");
|
||||
|
||||
@ -1664,7 +1655,7 @@ static int tree_content_get(
|
||||
for (i = 0; i < t->entry_count; i++) {
|
||||
e = t->entries[i];
|
||||
if (e->name->str_len == n && !strncmp_icase(p, e->name->str_dat, n)) {
|
||||
if (!slash1)
|
||||
if (!*slash1)
|
||||
goto found_entry;
|
||||
if (!S_ISDIR(e->versions[1].mode))
|
||||
return 0;
|
||||
|
Reference in New Issue
Block a user