Browse Source

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>
maint
Rohit Mani 11 years ago committed by Junio C Hamano
parent
commit
2c5495f7b6
  1. 4
      archive.c
  2. 15
      cache-tree.c
  3. 9
      diff.c
  4. 33
      fast-import.c
  5. 11
      match-trees.c
  6. 5
      parse-options.c
  7. 5
      pretty.c
  8. 4
      remote-testsvn.c
  9. 7
      ws.c

4
archive.c

@ -259,8 +259,8 @@ static void parse_treeish_arg(const char **argv,
/* Remotes are only allowed to fetch actual refs */ /* Remotes are only allowed to fetch actual refs */
if (remote) { if (remote) {
char *ref = NULL; char *ref = NULL;
const char *colon = strchr(name, ':'); const char *colon = strchrnul(name, ':');
int refnamelen = colon ? colon - name : strlen(name); int refnamelen = colon - name;


if (!dwim_ref(name, refnamelen, sha1, &ref)) if (!dwim_ref(name, refnamelen, sha1, &ref))
die("no such ref: %.*s", refnamelen, name); die("no such ref: %.*s", refnamelen, name);

15
cache-tree.c

@ -121,11 +121,11 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)


if (!it) if (!it)
return; return;
slash = strchr(path, '/'); slash = strchrnul(path, '/');
namelen = slash - path;
it->entry_count = -1; it->entry_count = -1;
if (!slash) { if (!*slash) {
int pos; int pos;
namelen = strlen(path);
pos = subtree_pos(it, path, namelen); pos = subtree_pos(it, path, namelen);
if (0 <= pos) { if (0 <= pos) {
cache_tree_free(&it->down[pos]->cache_tree); cache_tree_free(&it->down[pos]->cache_tree);
@ -143,7 +143,6 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
} }
return; return;
} }
namelen = slash - path;
down = find_subtree(it, path, namelen, 0); down = find_subtree(it, path, namelen, 0);
if (down) if (down)
cache_tree_invalidate_path(down->cache_tree, slash + 1); cache_tree_invalidate_path(down->cache_tree, slash + 1);
@ -554,9 +553,7 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat
const char *slash; const char *slash;
struct cache_tree_sub *sub; struct cache_tree_sub *sub;


slash = strchr(path, '/'); slash = strchrnul(path, '/');
if (!slash)
slash = path + strlen(path);
/* between path and slash is the name of the /* between path and slash is the name of the
* subtree to look for. * subtree to look for.
*/ */
@ -564,10 +561,10 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat
if (!sub) if (!sub)
return NULL; return NULL;
it = sub->cache_tree; it = sub->cache_tree;
if (slash) if (*slash)
while (*slash && *slash == '/') while (*slash && *slash == '/')
slash++; slash++;
if (!slash || !*slash) if (!*slash)
return it; /* prefix ended with slashes */ return it; /* prefix ended with slashes */
path = slash; path = slash;
} }

9
diff.c

@ -3365,14 +3365,11 @@ static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *va
if (c != '-') if (c != '-')
return 0; return 0;
arg++; arg++;
eq = strchr(arg, '='); eq = strchrnul(arg, '=');
if (eq) len = eq - arg;
len = eq - arg;
else
len = strlen(arg);
if (!len || strncmp(arg, arg_long, len)) if (!len || strncmp(arg, arg_long, len))
return 0; return 0;
if (eq) { if (*eq) {
int n; int n;
char *end; char *end;
if (!isdigit(*++eq)) if (!isdigit(*++eq))

33
fast-import.c

@ -1485,14 +1485,11 @@ static int tree_content_set(
unsigned int i, n; unsigned int i, n;
struct tree_entry *e; struct tree_entry *e;


slash1 = strchr(p, '/'); slash1 = strchrnul(p, '/');
if (slash1) n = slash1 - p;
n = slash1 - p;
else
n = strlen(p);
if (!n) if (!n)
die("Empty path component found in input"); 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"); die("Non-directories cannot have subtrees");


if (!root->tree) if (!root->tree)
@ -1501,7 +1498,7 @@ static int tree_content_set(
for (i = 0; i < t->entry_count; i++) { for (i = 0; i < t->entry_count; i++) {
e = t->entries[i]; e = t->entries[i];
if (e->name->str_len == n && !strncmp_icase(p, e->name->str_dat, n)) { if (e->name->str_len == n && !strncmp_icase(p, e->name->str_dat, n)) {
if (!slash1) { if (!*slash1) {
if (!S_ISDIR(mode) if (!S_ISDIR(mode)
&& e->versions[1].mode == mode && e->versions[1].mode == mode
&& !hashcmp(e->versions[1].sha1, sha1)) && !hashcmp(e->versions[1].sha1, sha1))
@ -1552,7 +1549,7 @@ static int tree_content_set(
e->versions[0].mode = 0; e->versions[0].mode = 0;
hashclr(e->versions[0].sha1); hashclr(e->versions[0].sha1);
t->entries[t->entry_count++] = e; t->entries[t->entry_count++] = e;
if (slash1) { if (*slash1) {
e->tree = new_tree_content(8); e->tree = new_tree_content(8);
e->versions[1].mode = S_IFDIR; e->versions[1].mode = S_IFDIR;
tree_content_set(e, slash1 + 1, sha1, mode, subtree); tree_content_set(e, slash1 + 1, sha1, mode, subtree);
@ -1576,11 +1573,8 @@ static int tree_content_remove(
unsigned int i, n; unsigned int i, n;
struct tree_entry *e; struct tree_entry *e;


slash1 = strchr(p, '/'); slash1 = strchrnul(p, '/');
if (slash1) n = slash1 - p;
n = slash1 - p;
else
n = strlen(p);


if (!root->tree) if (!root->tree)
load_tree(root); load_tree(root);
@ -1594,7 +1588,7 @@ static int tree_content_remove(
for (i = 0; i < t->entry_count; i++) { for (i = 0; i < t->entry_count; i++) {
e = t->entries[i]; e = t->entries[i];
if (e->name->str_len == n && !strncmp_icase(p, e->name->str_dat, n)) { 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 * If p names a file in some subdirectory, and a
* file or symlink matching the name of the * file or symlink matching the name of the
@ -1602,7 +1596,7 @@ static int tree_content_remove(
* exist and need not be deleted. * exist and need not be deleted.
*/ */
return 1; return 1;
if (!slash1 || !S_ISDIR(e->versions[1].mode)) if (!*slash1 || !S_ISDIR(e->versions[1].mode))
goto del_entry; goto del_entry;
if (!e->tree) if (!e->tree)
load_tree(e); load_tree(e);
@ -1644,11 +1638,8 @@ static int tree_content_get(
unsigned int i, n; unsigned int i, n;
struct tree_entry *e; struct tree_entry *e;


slash1 = strchr(p, '/'); slash1 = strchrnul(p, '/');
if (slash1) n = slash1 - p;
n = slash1 - p;
else
n = strlen(p);
if (!n && !allow_root) if (!n && !allow_root)
die("Empty path component found in input"); die("Empty path component found in input");


@ -1664,7 +1655,7 @@ static int tree_content_get(
for (i = 0; i < t->entry_count; i++) { for (i = 0; i < t->entry_count; i++) {
e = t->entries[i]; e = t->entries[i];
if (e->name->str_len == n && !strncmp_icase(p, e->name->str_dat, n)) { if (e->name->str_len == n && !strncmp_icase(p, e->name->str_dat, n)) {
if (!slash1) if (!*slash1)
goto found_entry; goto found_entry;
if (!S_ISDIR(e->versions[1].mode)) if (!S_ISDIR(e->versions[1].mode))
return 0; return 0;

11
match-trees.c

@ -182,13 +182,10 @@ static int splice_tree(const unsigned char *hash1,
enum object_type type; enum object_type type;
int status; int status;


subpath = strchr(prefix, '/'); subpath = strchrnul(prefix, '/');
if (!subpath) toplen = subpath - prefix;
toplen = strlen(prefix); if (*subpath)
else {
toplen = subpath - prefix;
subpath++; subpath++;
}


buf = read_sha1_file(hash1, &type, &sz); buf = read_sha1_file(hash1, &type, &sz);
if (!buf) if (!buf)
@ -215,7 +212,7 @@ static int splice_tree(const unsigned char *hash1,
if (!rewrite_here) if (!rewrite_here)
die("entry %.*s not found in tree %s", die("entry %.*s not found in tree %s",
toplen, prefix, sha1_to_hex(hash1)); toplen, prefix, sha1_to_hex(hash1));
if (subpath) { if (*subpath) {
status = splice_tree(rewrite_here, subpath, hash2, subtree); status = splice_tree(rewrite_here, subpath, hash2, subtree);
if (status) if (status)
return status; return status;

5
parse-options.c

@ -223,13 +223,10 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
const struct option *options) const struct option *options)
{ {
const struct option *all_opts = options; const struct option *all_opts = options;
const char *arg_end = strchr(arg, '='); const char *arg_end = strchrnul(arg, '=');
const struct option *abbrev_option = NULL, *ambiguous_option = NULL; const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
int abbrev_flags = 0, ambiguous_flags = 0; int abbrev_flags = 0, ambiguous_flags = 0;


if (!arg_end)
arg_end = arg + strlen(arg);

for (; options->type != OPTION_END; options++) { for (; options->type != OPTION_END; options++) {
const char *rest, *long_name = options->long_name; const char *rest, *long_name = options->long_name;
int flags = 0, opt_flags = 0; int flags = 0, opt_flags = 0;

5
pretty.c

@ -549,14 +549,13 @@ static char *get_header(const struct commit *commit, const char *msg,
const char *line = msg; const char *line = msg;


while (line) { while (line) {
const char *eol = strchr(line, '\n'), *next; const char *eol = strchrnul(line, '\n'), *next;


if (line == eol) if (line == eol)
return NULL; return NULL;
if (!eol) { if (!*eol) {
warning("malformed commit (header is missing newline): %s", warning("malformed commit (header is missing newline): %s",
sha1_to_hex(commit->object.sha1)); sha1_to_hex(commit->object.sha1));
eol = line + strlen(line);
next = NULL; next = NULL;
} else } else
next = eol + 1; next = eol + 1;

4
remote-testsvn.c

@ -78,8 +78,8 @@ static int parse_rev_note(const char *msg, struct rev_note *res)
size_t len; size_t len;


while (*msg) { while (*msg) {
end = strchr(msg, '\n'); end = strchrnul(msg, '\n');
len = end ? end - msg : strlen(msg); len = end - msg;


key = "Revision-number: "; key = "Revision-number: ";
if (starts_with(msg, key)) { if (starts_with(msg, key)) {

7
ws.c

@ -33,11 +33,8 @@ unsigned parse_whitespace_rule(const char *string)
int negated = 0; int negated = 0;


string = string + strspn(string, ", \t\n\r"); string = string + strspn(string, ", \t\n\r");
ep = strchr(string, ','); ep = strchrnul(string, ',');
if (!ep) len = ep - string;
len = strlen(string);
else
len = ep - string;


if (*string == '-') { if (*string == '-') {
negated = 1; negated = 1;

Loading…
Cancel
Save