Add git_path_icmp to case-insensitive path cmp This adds git_path_icmp to complement git_path_cmp.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
diff --git a/src/path.c b/src/path.c
index 0fd367e..5de58cc 100644
--- a/src/path.c
+++ b/src/path.c
@@ -701,6 +701,30 @@ int git_path_cmp(
return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
}
+int git_path_icmp(
+ const char *name1, size_t len1, int isdir1,
+ const char *name2, size_t len2, int isdir2)
+{
+ unsigned char c1, c2;
+ size_t len = len1 < len2 ? len1 : len2;
+ int cmp;
+
+ cmp = strncasecmp(name1, name2, len);
+ if (cmp)
+ return cmp;
+
+ c1 = name1[len];
+ c2 = name2[len];
+
+ if (c1 == '\0' && isdir1)
+ c1 = '/';
+
+ if (c2 == '\0' && isdir2)
+ c2 = '/';
+
+ return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
+}
+
int git_path_direach(
git_buf *path,
int (*fn)(void *, git_buf *),
diff --git a/src/path.h b/src/path.h
index de0a40b..feefd65 100644
--- a/src/path.h
+++ b/src/path.h
@@ -261,12 +261,17 @@ extern int git_path_direach(
void *state);
/**
- * Sort function to order two paths.
+ * Sort function to order two paths
*/
extern int git_path_cmp(
const char *name1, size_t len1, int isdir1,
const char *name2, size_t len2, int isdir2);
+/** Path sort function that is case insensitive */
+extern int git_path_icmp(
+ const char *name1, size_t len1, int isdir1,
+ const char *name2, size_t len2, int isdir2);
+
/**
* Invoke callback up path directory by directory until the ceiling is
* reached (inclusive of a final call at the root_path).