diff options
author | Rohit Mani <rohit.mani@outlook.com> | 2014-03-07 22:48:31 -0800 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2014-03-10 08:35:30 -0700 |
commit | 2c5495f7b60d6ddcd6a411b48d2f6dbc4a24717a (patch) | |
tree | 9900454e2b547e2fee8e9e67b39ced68ea0c650e /fast-import.c | |
parent | Git 1.9.0 (diff) | |
download | tgif-2c5495f7b60d6ddcd6a411b48d2f6dbc4a24717a.tar.xz |
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>
Diffstat (limited to 'fast-import.c')
-rw-r--r-- | fast-import.c | 33 |
1 files changed, 12 insertions, 21 deletions
diff --git a/fast-import.c b/fast-import.c index 4fd18a3574..fb4738d373 100644 --- a/fast-import.c +++ b/fast-import.c @@ -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; |