Skip to content

Commit a6db3fb

Browse files
jeffhostetlergitster
authored andcommitted
read-cache: add strcmp_offset function
Add strcmp_offset() function to also return the offset of the first change. Add unit test and helper to verify. Signed-off-by: Jeff Hostetler <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cf11a67 commit a6db3fb

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ TEST_PROGRAMS_NEED_X += test-scrap-cache-tree
637637
TEST_PROGRAMS_NEED_X += test-sha1
638638
TEST_PROGRAMS_NEED_X += test-sha1-array
639639
TEST_PROGRAMS_NEED_X += test-sigchain
640+
TEST_PROGRAMS_NEED_X += test-strcmp-offset
640641
TEST_PROGRAMS_NEED_X += test-string-list
641642
TEST_PROGRAMS_NEED_X += test-submodule-config
642643
TEST_PROGRAMS_NEED_X += test-subprocess

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ extern int write_locked_index(struct index_state *, struct lock_file *lock, unsi
595595
extern int discard_index(struct index_state *);
596596
extern int unmerged_index(const struct index_state *);
597597
extern int verify_path(const char *path);
598+
extern int strcmp_offset(const char *s1, const char *s2, size_t *first_change);
598599
extern int index_dir_exists(struct index_state *istate, const char *name, int namelen);
599600
extern void adjust_dirname_case(struct index_state *istate, char *name);
600601
extern struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int igncase);

read-cache.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,26 @@ static int has_file_name(struct index_state *istate,
887887
return retval;
888888
}
889889

890+
891+
/*
892+
* Like strcmp(), but also return the offset of the first change.
893+
* If strings are equal, return the length.
894+
*/
895+
int strcmp_offset(const char *s1, const char *s2, size_t *first_change)
896+
{
897+
size_t k;
898+
899+
if (!first_change)
900+
return strcmp(s1, s2);
901+
902+
for (k = 0; s1[k] == s2[k]; k++)
903+
if (s1[k] == '\0')
904+
break;
905+
906+
*first_change = k;
907+
return (unsigned char)s1[k] - (unsigned char)s2[k];
908+
}
909+
890910
/*
891911
* Do we have another file with a pathname that is a proper
892912
* subset of the name we're trying to add?

t/helper/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
/test-sha1
2727
/test-sha1-array
2828
/test-sigchain
29+
/test-strcmp-offset
2930
/test-string-list
3031
/test-submodule-config
3132
/test-subprocess

t/helper/test-strcmp-offset.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "cache.h"
2+
3+
int cmd_main(int argc, const char **argv)
4+
{
5+
int result;
6+
size_t offset;
7+
8+
if (!argv[1] || !argv[2])
9+
die("usage: %s <string1> <string2>", argv[0]);
10+
11+
result = strcmp_offset(argv[1], argv[2], &offset);
12+
13+
/*
14+
* Because differnt CRTs behave differently, only rely on signs
15+
* of the result values.
16+
*/
17+
result = (result < 0 ? -1 :
18+
result > 0 ? 1 :
19+
0);
20+
printf("%d %"PRIuMAX"\n", result, (uintmax_t)offset);
21+
return 0;
22+
}

t/t0065-strcmp-offset.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
3+
test_description='Test strcmp_offset functionality'
4+
5+
. ./test-lib.sh
6+
7+
while read s1 s2 expect
8+
do
9+
test_expect_success "strcmp_offset($s1, $s2)" '
10+
echo "$expect" >expect &&
11+
test-strcmp-offset "$s1" "$s2" >actual &&
12+
test_cmp expect actual
13+
'
14+
done <<-EOF
15+
abc abc 0 3
16+
abc def -1 0
17+
abc abz -1 2
18+
abc abcdef -1 3
19+
EOF
20+
21+
test_done

0 commit comments

Comments
 (0)