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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Feb 5, 2023 at 11:42 UTC
e65b868d074a38b3049529064bb05df53948761f
1 file changed
+5
-8
builtin/pack-objects.c
+5
-8
@@ -1708,17 +1708,14 @@ static void pbase_tree_put(struct pbase_tree_cache *cache)
1708
free(cache);
1709
}
1710
1711
-static int name_cmp_len(const char *name)
1711
+static size_t name_cmp_len(const char *name)
1712
{
1713
- int i;
1714
- for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
1715
- ;
1716
- return i;
1713
+ return strcspn(name, "\n/");
1714
}
1715
1716
static void add_pbase_object(struct tree_desc *tree,
1717
const char *name,
1721
- int cmplen,
1718
+ size_t cmplen,
1719
const char *fullname)
1720
{
1721
struct name_entry entry;
@@ -1743,7 +1740,7 @@ static void add_pbase_object(struct tree_desc *tree,
1740
struct tree_desc sub;
1741
struct pbase_tree_cache *tree;
1742
const char *down = name+cmplen+1;
1746
- int downlen = name_cmp_len(down);
1743
+ size_t downlen = name_cmp_len(down);
1744
1745
tree = pbase_tree_get(&entry.oid);
1746
if (!tree)
@@ -1795,7 +1792,7 @@ static int check_pbase_path(unsigned hash)
1792
static void add_preferred_base_object(const char *name)
1793
{
1794
struct pbase_tree *it;
1798
- int cmplen;
1795
+ size_t cmplen;
1796
unsigned hash = pack_name_hash(name);
1797
1798
if (!num_preferred_base || check_pbase_path(hash))