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 <jeffhost@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff Hostetler committed
Apr 14, 2017 at 19:12 UTC
a6db3fbb6e4ec11695e2a3af8bc2cb9119cb1941
6 files changed
+66
Makefile
+1
@@ -637,6 +637,7 @@ TEST_PROGRAMS_NEED_X += test-scrap-cache-tree
637
TEST_PROGRAMS_NEED_X += test-sha1
638
TEST_PROGRAMS_NEED_X += test-sha1-array
639
TEST_PROGRAMS_NEED_X += test-sigchain
640
+TEST_PROGRAMS_NEED_X += test-strcmp-offset
641
TEST_PROGRAMS_NEED_X += test-string-list
642
TEST_PROGRAMS_NEED_X += test-submodule-config
643
TEST_PROGRAMS_NEED_X += test-subprocess
cache.h
+1
@@ -595,6 +595,7 @@ extern int write_locked_index(struct index_state *, struct lock_file *lock, unsi
595
extern int discard_index(struct index_state *);
596
extern int unmerged_index(const struct index_state *);
597
extern int verify_path(const char *path);
598
+extern int strcmp_offset(const char *s1, const char *s2, size_t *first_change);
599
extern int index_dir_exists(struct index_state *istate, const char *name, int namelen);
600
extern void adjust_dirname_case(struct index_state *istate, char *name);
601
extern struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int igncase);
read-cache.c
+20
@@ -887,6 +887,26 @@ static int has_file_name(struct index_state *istate,
887
return retval;
888
}
889
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
+
910
/*
911
* Do we have another file with a pathname that is a proper
912
* subset of the name we're trying to add?
t/helper/.gitignore
+1
@@ -26,6 +26,7 @@
26
/test-sha1
27
/test-sha1-array
28
/test-sigchain
29
+/test-strcmp-offset
30
/test-string-list
31
/test-submodule-config
32
/test-subprocess
t/helper/test-strcmp-offset.c
new
+22
@@ -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
new
+21
@@ -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