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>
Rohit Mani committed
Mar 7, 2014 at 22:48 UTC
2c5495f7b60d6ddcd6a411b48d2f6dbc4a24717a
9 files changed
+34
-59
archive.c
+2
-2
@@ -259,8 +259,8 @@ static void parse_treeish_arg(const char **argv,
259
/* Remotes are only allowed to fetch actual refs */
260
if (remote) {
261
char *ref = NULL;
262
- const char *colon = strchr(name, ':');
263
- int refnamelen = colon ? colon - name : strlen(name);
262
+ const char *colon = strchrnul(name, ':');
263
+ int refnamelen = colon - name;
264
265
if (!dwim_ref(name, refnamelen, sha1, &ref))
266
die("no such ref: %.*s", refnamelen, name);
cache-tree.c
+6
-9
@@ -121,11 +121,11 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
121
122
if (!it)
123
return;
124
- slash = strchr(path, '/');
124
+ slash = strchrnul(path, '/');
125
+ namelen = slash - path;
126
it->entry_count = -1;
126
- if (!slash) {
127
+ if (!*slash) {
128
int pos;
128
- namelen = strlen(path);
129
pos = subtree_pos(it, path, namelen);
130
if (0 <= pos) {
131
cache_tree_free(&it->down[pos]->cache_tree);
@@ -143,7 +143,6 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
143
}
144
return;
145
}
146
- namelen = slash - path;
146
down = find_subtree(it, path, namelen, 0);
147
if (down)
148
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
553
const char *slash;
554
struct cache_tree_sub *sub;
555
557
- slash = strchr(path, '/');
558
- if (!slash)
559
- slash = path + strlen(path);
556
+ slash = strchrnul(path, '/');
557
/* between path and slash is the name of the
558
* subtree to look for.
559
*/
@@ -564,10 +561,10 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat
561
if (!sub)
562
return NULL;
563
it = sub->cache_tree;
567
- if (slash)
564
+ if (*slash)
565
while (*slash && *slash == '/')
566
slash++;
570
- if (!slash || !*slash)
567
+ if (!*slash)
568
return it; /* prefix ended with slashes */
569
path = slash;
570
}
diff.c
+3
-6
@@ -3365,14 +3365,11 @@ static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *va
3365
if (c != '-')
3366
return 0;
3367
arg++;
3368
- eq = strchr(arg, '=');
3369
- if (eq)
3370
- len = eq - arg;
3371
- else
3372
- len = strlen(arg);
3368
+ eq = strchrnul(arg, '=');
3369
+ len = eq - arg;
3370
if (!len || strncmp(arg, arg_long, len))
3371
return 0;
3375
- if (eq) {
3372
+ if (*eq) {
3373
int n;
3374
char *end;
3375
if (!isdigit(*++eq))
fast-import.c
+12
-21
@@ -1485,14 +1485,11 @@ static int tree_content_set(
1485
unsigned int i, n;
1486
struct tree_entry *e;
1487
1488
- slash1 = strchr(p, '/');
1489
- if (slash1)
1490
- n = slash1 - p;
1491
- else
1492
- n = strlen(p);
1488
+ slash1 = strchrnul(p, '/');
1489
+ n = slash1 - p;
1490
if (!n)
1491
die("Empty path component found in input");
1495
- if (!slash1 && !S_ISDIR(mode) && subtree)
1492
+ if (!*slash1 && !S_ISDIR(mode) && subtree)
1493
die("Non-directories cannot have subtrees");
1494
1495
if (!root->tree)
@@ -1501,7 +1498,7 @@ static int tree_content_set(
1498
for (i = 0; i < t->entry_count; i++) {
1499
e = t->entries[i];
1500
if (e->name->str_len == n && !strncmp_icase(p, e->name->str_dat, n)) {
1504
- if (!slash1) {
1501
+ if (!*slash1) {
1502
if (!S_ISDIR(mode)
1503
&& e->versions[1].mode == mode
1504
&& !hashcmp(e->versions[1].sha1, sha1))
@@ -1552,7 +1549,7 @@ static int tree_content_set(
1549
e->versions[0].mode = 0;
1550
hashclr(e->versions[0].sha1);
1551
t->entries[t->entry_count++] = e;
1555
- if (slash1) {
1552
+ if (*slash1) {
1553
e->tree = new_tree_content(8);
1554
e->versions[1].mode = S_IFDIR;
1555
tree_content_set(e, slash1 + 1, sha1, mode, subtree);
@@ -1576,11 +1573,8 @@ static int tree_content_remove(
1573
unsigned int i, n;
1574
struct tree_entry *e;
1575
1579
- slash1 = strchr(p, '/');
1580
- if (slash1)
1581
- n = slash1 - p;
1582
- else
1583
- n = strlen(p);
1576
+ slash1 = strchrnul(p, '/');
1577
+ n = slash1 - p;
1578
1579
if (!root->tree)
1580
load_tree(root);
@@ -1594,7 +1588,7 @@ static int tree_content_remove(
1588
for (i = 0; i < t->entry_count; i++) {
1589
e = t->entries[i];
1590
if (e->name->str_len == n && !strncmp_icase(p, e->name->str_dat, n)) {
1597
- if (slash1 && !S_ISDIR(e->versions[1].mode))
1591
+ if (*slash1 && !S_ISDIR(e->versions[1].mode))
1592
/*
1593
* If p names a file in some subdirectory, and a
1594
* file or symlink matching the name of the
@@ -1602,7 +1596,7 @@ static int tree_content_remove(
1596
* exist and need not be deleted.
1597
*/
1598
return 1;
1605
- if (!slash1 || !S_ISDIR(e->versions[1].mode))
1599
+ if (!*slash1 || !S_ISDIR(e->versions[1].mode))
1600
goto del_entry;
1601
if (!e->tree)
1602
load_tree(e);
@@ -1644,11 +1638,8 @@ static int tree_content_get(
1638
unsigned int i, n;
1639
struct tree_entry *e;
1640
1647
- slash1 = strchr(p, '/');
1648
- if (slash1)
1649
- n = slash1 - p;
1650
- else
1651
- n = strlen(p);
1641
+ slash1 = strchrnul(p, '/');
1642
+ n = slash1 - p;
1643
if (!n && !allow_root)
1644
die("Empty path component found in input");
1645
@@ -1664,7 +1655,7 @@ static int tree_content_get(
1655
for (i = 0; i < t->entry_count; i++) {
1656
e = t->entries[i];
1657
if (e->name->str_len == n && !strncmp_icase(p, e->name->str_dat, n)) {
1667
- if (!slash1)
1658
+ if (!*slash1)
1659
goto found_entry;
1660
if (!S_ISDIR(e->versions[1].mode))
1661
return 0;
match-trees.c
+4
-7
@@ -182,13 +182,10 @@ static int splice_tree(const unsigned char *hash1,
182
enum object_type type;
183
int status;
184
185
- subpath = strchr(prefix, '/');
186
- if (!subpath)
187
- toplen = strlen(prefix);
188
- else {
189
- toplen = subpath - prefix;
185
+ subpath = strchrnul(prefix, '/');
186
+ toplen = subpath - prefix;
187
+ if (*subpath)
188
subpath++;
191
- }
189
190
buf = read_sha1_file(hash1, &type, &sz);
191
if (!buf)
@@ -215,7 +212,7 @@ static int splice_tree(const unsigned char *hash1,
212
if (!rewrite_here)
213
die("entry %.*s not found in tree %s",
214
toplen, prefix, sha1_to_hex(hash1));
218
- if (subpath) {
215
+ if (*subpath) {
216
status = splice_tree(rewrite_here, subpath, hash2, subtree);
217
if (status)
218
return status;
parse-options.c
+1
-4
@@ -223,13 +223,10 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
223
const struct option *options)
224
{
225
const struct option *all_opts = options;
226
- const char *arg_end = strchr(arg, '=');
226
+ const char *arg_end = strchrnul(arg, '=');
227
const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
228
int abbrev_flags = 0, ambiguous_flags = 0;
229
230
- if (!arg_end)
231
- arg_end = arg + strlen(arg);
232
-
230
for (; options->type != OPTION_END; options++) {
231
const char *rest, *long_name = options->long_name;
232
int flags = 0, opt_flags = 0;
pretty.c
+2
-3
@@ -549,14 +549,13 @@ static char *get_header(const struct commit *commit, const char *msg,
549
const char *line = msg;
550
551
while (line) {
552
- const char *eol = strchr(line, '\n'), *next;
552
+ const char *eol = strchrnul(line, '\n'), *next;
553
554
if (line == eol)
555
return NULL;
556
- if (!eol) {
556
+ if (!*eol) {
557
warning("malformed commit (header is missing newline): %s",
558
sha1_to_hex(commit->object.sha1));
559
- eol = line + strlen(line);
559
next = NULL;
560
} else
561
next = eol + 1;
remote-testsvn.c
+2
-2
@@ -78,8 +78,8 @@ static int parse_rev_note(const char *msg, struct rev_note *res)
78
size_t len;
79
80
while (*msg) {
81
- end = strchr(msg, '\n');
82
- len = end ? end - msg : strlen(msg);
81
+ end = strchrnul(msg, '\n');
82
+ len = end - msg;
83
84
key = "Revision-number: ";
85
if (starts_with(msg, key)) {
ws.c
+2
-5
@@ -33,11 +33,8 @@ unsigned parse_whitespace_rule(const char *string)
33
int negated = 0;
34
35
string = string + strspn(string, ", \t\n\r");
36
- ep = strchr(string, ',');
37
- if (!ep)
38
- len = strlen(string);
39
- else
40
- len = ep - string;
36
+ ep = strchrnul(string, ',');
37
+ len = ep - string;
38
39
if (*string == '-') {
40
negated = 1;