Skip to content

Commit 5970723

Browse files
committed
Merge branch 'jm/dedup-name-compare'
* jm/dedup-name-compare: cleanup duplicate name_compare() functions name-hash.c: replace cache_name_compare() with memcmp(3)
2 parents e568572 + ccdd4a0 commit 5970723

File tree

6 files changed

+16
-35
lines changed

6 files changed

+16
-35
lines changed

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,7 @@ extern int validate_headref(const char *ref);
999999

10001000
extern int base_name_compare(const char *name1, int len1, int mode1, const char *name2, int len2, int mode2);
10011001
extern int df_name_compare(const char *name1, int len1, int mode1, const char *name2, int len2, int mode2);
1002-
extern int cache_name_compare(const char *name1, int len1, const char *name2, int len2);
1002+
extern int name_compare(const char *name1, size_t len1, const char *name2, size_t len2);
10031003
extern int cache_name_stage_compare(const char *name1, int len1, int stage1, const char *name2, int len2, int stage2);
10041004

10051005
extern void *read_object_with_reference(const unsigned char *sha1,

dir.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,8 +1354,7 @@ static int cmp_name(const void *p1, const void *p2)
13541354
const struct dir_entry *e1 = *(const struct dir_entry **)p1;
13551355
const struct dir_entry *e2 = *(const struct dir_entry **)p2;
13561356

1357-
return cache_name_compare(e1->name, e1->len,
1358-
e2->name, e2->len);
1357+
return name_compare(e1->name, e1->len, e2->name, e2->len);
13591358
}
13601359

13611360
static struct path_simplify *create_simplify(const char **pathspec)

name-hash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ static int same_name(const struct cache_entry *ce, const char *name, int namelen
179179
* Always do exact compare, even if we want a case-ignoring comparison;
180180
* we do the quick exact one first, because it will be the common case.
181181
*/
182-
if (len == namelen && !cache_name_compare(name, namelen, ce->name, len))
182+
if (len == namelen && !memcmp(name, ce->name, len))
183183
return 1;
184184

185185
if (!icase)

read-cache.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -422,18 +422,26 @@ int df_name_compare(const char *name1, int len1, int mode1,
422422
return c1 - c2;
423423
}
424424

425-
int cache_name_stage_compare(const char *name1, int len1, int stage1, const char *name2, int len2, int stage2)
425+
int name_compare(const char *name1, size_t len1, const char *name2, size_t len2)
426426
{
427-
int len = len1 < len2 ? len1 : len2;
428-
int cmp;
429-
430-
cmp = memcmp(name1, name2, len);
427+
size_t min_len = (len1 < len2) ? len1 : len2;
428+
int cmp = memcmp(name1, name2, min_len);
431429
if (cmp)
432430
return cmp;
433431
if (len1 < len2)
434432
return -1;
435433
if (len1 > len2)
436434
return 1;
435+
return 0;
436+
}
437+
438+
int cache_name_stage_compare(const char *name1, int len1, int stage1, const char *name2, int len2, int stage2)
439+
{
440+
int cmp;
441+
442+
cmp = name_compare(name1, len1, name2, len2);
443+
if (cmp)
444+
return cmp;
437445

438446
if (stage1 < stage2)
439447
return -1;
@@ -442,11 +450,6 @@ int cache_name_stage_compare(const char *name1, int len1, int stage1, const char
442450
return 0;
443451
}
444452

445-
int cache_name_compare(const char *name1, int len1, const char *name2, int len2)
446-
{
447-
return cache_name_stage_compare(name1, len1, 0, name2, len2, 0);
448-
}
449-
450453
static int index_name_stage_pos(const struct index_state *istate, const char *name, int namelen, int stage)
451454
{
452455
int first, last;

tree-walk.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,6 @@ struct tree_desc_x {
144144
struct tree_desc_skip *skip;
145145
};
146146

147-
static int name_compare(const char *a, int a_len,
148-
const char *b, int b_len)
149-
{
150-
int len = (a_len < b_len) ? a_len : b_len;
151-
int cmp = memcmp(a, b, len);
152-
if (cmp)
153-
return cmp;
154-
return (a_len - b_len);
155-
}
156-
157147
static int check_entry_match(const char *a, int a_len, const char *b, int b_len)
158148
{
159149
/*

unpack-trees.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -622,17 +622,6 @@ static int unpack_failed(struct unpack_trees_options *o, const char *message)
622622
return -1;
623623
}
624624

625-
/* NEEDSWORK: give this a better name and share with tree-walk.c */
626-
static int name_compare(const char *a, int a_len,
627-
const char *b, int b_len)
628-
{
629-
int len = (a_len < b_len) ? a_len : b_len;
630-
int cmp = memcmp(a, b, len);
631-
if (cmp)
632-
return cmp;
633-
return (a_len - b_len);
634-
}
635-
636625
/*
637626
* The tree traversal is looking at name p. If we have a matching entry,
638627
* return it. If name p is a directory in the index, do not return

0 commit comments

Comments
 (0)