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 <jmmahler@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeremiah Mahler committed Jun 19, 2014 at 19:06 UTC ccdd4a0f3c7c550526a980e93156f25a956497bc
5 files changed +15 -34
cache.h
+1 -1
@@ -999,7 +999,7 @@ extern int validate_headref(const char *ref);
999
1000 extern int base_name_compare(const char *name1, int len1, int mode1, const char *name2, int len2, int mode2);
1001 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);
1003 extern int cache_name_stage_compare(const char *name1, int len1, int stage1, const char *name2, int len2, int stage2);
1004
1005 extern void *read_object_with_reference(const unsigned char *sha1,
dir.c
+1 -2
@@ -1354,8 +1354,7 @@ static int cmp_name(const void *p1, const void *p2)
1354 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
1355 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
1356
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);
1358 }
1359
1360 static struct path_simplify *create_simplify(const char **pathspec)
read-cache.c
+13 -10
@@ -422,18 +422,26 @@ int df_name_compare(const char *name1, int len1, int mode1,
422 return c1 - c2;
423 }
424
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)
426 {
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);
429 if (cmp)
430 return cmp;
431 if (len1 < len2)
432 return -1;
433 if (len1 > len2)
434 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;
445
446 if (stage1 < stage2)
447 return -1;
@@ -442,11 +450,6 @@ int cache_name_stage_compare(const char *name1, int len1, int stage1, const char
450 return 0;
451 }
452
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 -
453 static int index_name_stage_pos(const struct index_state *istate, const char *name, int namelen, int stage)
454 {
455 int first, last;
tree-walk.c
-10
@@ -144,16 +144,6 @@ struct tree_desc_x {
144 struct tree_desc_skip *skip;
145 };
146
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 -
147 static int check_entry_match(const char *a, int a_len, const char *b, int b_len)
148 {
149 /*
unpack-trees.c
-11
@@ -622,17 +622,6 @@ static int unpack_failed(struct unpack_trees_options *o, const char *message)
622 return -1;
623 }
624
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 -
625 /*
626 * The tree traversal is looking at name p. If we have a matching entry,
627 * return it. If name p is a directory in the index, do not return