Skip to content

Commit ccdd4a0

Browse files
jmahlergitster
authored andcommitted
cleanup duplicate name_compare() functions
We often represent our strings as a counted string, i.e. a pair of the pointer to the beginning of the string and its length, and the string may not be NUL terminated to that length. To compare a pair of such counted strings, unpack-trees.c and read-cache.c implement their own name_compare() functions identically. In addition, the cache_name_compare() function in read-cache.c is nearly identical. The only difference is when one string is the prefix of the other string, in which case name_compare() returns -1/+1 to show which one is longer, and cache_name_compare() returns the difference of the lengths to show the same information. Unify these three functions by using the implementation from cache_name_compare(). This does not make any difference to the existing and future callers, as they must be paying attention only to the sign of the returned value (and not the magnitude) because the original implementations of these two functions return values returned by memcmp(3) when the one string is not a prefix of the other string, and the only thing memcmp(3) guarantees its callers is the sign of the returned value, not the magnitude. Signed-off-by: Jeremiah Mahler <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent be99ec9 commit ccdd4a0

File tree

5 files changed

+15
-34
lines changed

5 files changed

+15
-34
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)

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)