Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 2c5495f

Browse files
RohitManigitster
authored andcommitted
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 <[email protected]> Signed-off-by: Rohit Mani <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5f95c9f commit 2c5495f

File tree

9 files changed

+34
-59
lines changed

9 files changed

+34
-59
lines changed

archive.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ static void parse_treeish_arg(const char **argv,
259259
/* Remotes are only allowed to fetch actual refs */
260260
if (remote) {
261261
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;
264264

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

cache-tree.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
121121

122122
if (!it)
123123
return;
124-
slash = strchr(path, '/');
124+
slash = strchrnul(path, '/');
125+
namelen = slash - path;
125126
it->entry_count = -1;
126-
if (!slash) {
127+
if (!*slash) {
127128
int pos;
128-
namelen = strlen(path);
129129
pos = subtree_pos(it, path, namelen);
130130
if (0 <= pos) {
131131
cache_tree_free(&it->down[pos]->cache_tree);
@@ -143,7 +143,6 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
143143
}
144144
return;
145145
}
146-
namelen = slash - path;
147146
down = find_subtree(it, path, namelen, 0);
148147
if (down)
149148
cache_tree_invalidate_path(down->cache_tree, slash + 1);
@@ -554,20 +553,18 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat
554553
const char *slash;
555554
struct cache_tree_sub *sub;
556555

557-
slash = strchr(path, '/');
558-
if (!slash)
559-
slash = path + strlen(path);
556+
slash = strchrnul(path, '/');
560557
/* between path and slash is the name of the
561558
* subtree to look for.
562559
*/
563560
sub = find_subtree(it, path, slash - path, 0);
564561
if (!sub)
565562
return NULL;
566563
it = sub->cache_tree;
567-
if (slash)
564+
if (*slash)
568565
while (*slash && *slash == '/')
569566
slash++;
570-
if (!slash || !*slash)
567+
if (!*slash)
571568
return it; /* prefix ended with slashes */
572569
path = slash;
573570
}

diff.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3365,14 +3365,11 @@ static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *va
33653365
if (c != '-')
33663366
return 0;
33673367
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;
33733370
if (!len || strncmp(arg, arg_long, len))
33743371
return 0;
3375-
if (eq) {
3372+
if (*eq) {
33763373
int n;
33773374
char *end;
33783375
if (!isdigit(*++eq))

fast-import.c

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,14 +1485,11 @@ static int tree_content_set(
14851485
unsigned int i, n;
14861486
struct tree_entry *e;
14871487

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;
14931490
if (!n)
14941491
die("Empty path component found in input");
1495-
if (!slash1 && !S_ISDIR(mode) && subtree)
1492+
if (!*slash1 && !S_ISDIR(mode) && subtree)
14961493
die("Non-directories cannot have subtrees");
14971494

14981495
if (!root->tree)
@@ -1501,7 +1498,7 @@ static int tree_content_set(
15011498
for (i = 0; i < t->entry_count; i++) {
15021499
e = t->entries[i];
15031500
if (e->name->str_len == n && !strncmp_icase(p, e->name->str_dat, n)) {
1504-
if (!slash1) {
1501+
if (!*slash1) {
15051502
if (!S_ISDIR(mode)
15061503
&& e->versions[1].mode == mode
15071504
&& !hashcmp(e->versions[1].sha1, sha1))
@@ -1552,7 +1549,7 @@ static int tree_content_set(
15521549
e->versions[0].mode = 0;
15531550
hashclr(e->versions[0].sha1);
15541551
t->entries[t->entry_count++] = e;
1555-
if (slash1) {
1552+
if (*slash1) {
15561553
e->tree = new_tree_content(8);
15571554
e->versions[1].mode = S_IFDIR;
15581555
tree_content_set(e, slash1 + 1, sha1, mode, subtree);
@@ -1576,11 +1573,8 @@ static int tree_content_remove(
15761573
unsigned int i, n;
15771574
struct tree_entry *e;
15781575

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;
15841578

15851579
if (!root->tree)
15861580
load_tree(root);
@@ -1594,15 +1588,15 @@ static int tree_content_remove(
15941588
for (i = 0; i < t->entry_count; i++) {
15951589
e = t->entries[i];
15961590
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))
15981592
/*
15991593
* If p names a file in some subdirectory, and a
16001594
* file or symlink matching the name of the
16011595
* parent directory of p exists, then p cannot
16021596
* exist and need not be deleted.
16031597
*/
16041598
return 1;
1605-
if (!slash1 || !S_ISDIR(e->versions[1].mode))
1599+
if (!*slash1 || !S_ISDIR(e->versions[1].mode))
16061600
goto del_entry;
16071601
if (!e->tree)
16081602
load_tree(e);
@@ -1644,11 +1638,8 @@ static int tree_content_get(
16441638
unsigned int i, n;
16451639
struct tree_entry *e;
16461640

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;
16521643
if (!n && !allow_root)
16531644
die("Empty path component found in input");
16541645

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

match-trees.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,10 @@ static int splice_tree(const unsigned char *hash1,
182182
enum object_type type;
183183
int status;
184184

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)
190188
subpath++;
191-
}
192189

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

parse-options.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,10 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
223223
const struct option *options)
224224
{
225225
const struct option *all_opts = options;
226-
const char *arg_end = strchr(arg, '=');
226+
const char *arg_end = strchrnul(arg, '=');
227227
const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
228228
int abbrev_flags = 0, ambiguous_flags = 0;
229229

230-
if (!arg_end)
231-
arg_end = arg + strlen(arg);
232-
233230
for (; options->type != OPTION_END; options++) {
234231
const char *rest, *long_name = options->long_name;
235232
int flags = 0, opt_flags = 0;

pretty.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,14 +549,13 @@ static char *get_header(const struct commit *commit, const char *msg,
549549
const char *line = msg;
550550

551551
while (line) {
552-
const char *eol = strchr(line, '\n'), *next;
552+
const char *eol = strchrnul(line, '\n'), *next;
553553

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

remote-testsvn.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ static int parse_rev_note(const char *msg, struct rev_note *res)
7878
size_t len;
7979

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

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

ws.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,8 @@ unsigned parse_whitespace_rule(const char *string)
3333
int negated = 0;
3434

3535
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;
4138

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

0 commit comments

Comments
 (0)