Skip to content

Commit e65b868

Browse files
rscharfegitster
authored andcommitted
pack-objects: use strcspn(3) in name_cmp_len()
Call strcspn(3) to find the length of a string terminated by NUL, NL or slash instead of open-coding it. Adopt its return type, size_t, to support strings of arbitrary length. Use that type in callers as well for variables and function parameters that receive the return value. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1b4a38d commit e65b868

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

builtin/pack-objects.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,17 +1708,14 @@ static void pbase_tree_put(struct pbase_tree_cache *cache)
17081708
free(cache);
17091709
}
17101710

1711-
static int name_cmp_len(const char *name)
1711+
static size_t name_cmp_len(const char *name)
17121712
{
1713-
int i;
1714-
for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
1715-
;
1716-
return i;
1713+
return strcspn(name, "\n/");
17171714
}
17181715

17191716
static void add_pbase_object(struct tree_desc *tree,
17201717
const char *name,
1721-
int cmplen,
1718+
size_t cmplen,
17221719
const char *fullname)
17231720
{
17241721
struct name_entry entry;
@@ -1743,7 +1740,7 @@ static void add_pbase_object(struct tree_desc *tree,
17431740
struct tree_desc sub;
17441741
struct pbase_tree_cache *tree;
17451742
const char *down = name+cmplen+1;
1746-
int downlen = name_cmp_len(down);
1743+
size_t downlen = name_cmp_len(down);
17471744

17481745
tree = pbase_tree_get(&entry.oid);
17491746
if (!tree)
@@ -1795,7 +1792,7 @@ static int check_pbase_path(unsigned hash)
17951792
static void add_preferred_base_object(const char *name)
17961793
{
17971794
struct pbase_tree *it;
1798-
int cmplen;
1795+
size_t cmplen;
17991796
unsigned hash = pack_name_hash(name);
18001797

18011798
if (!num_preferred_base || check_pbase_path(hash))

0 commit comments

Comments
 (0)