git__strcasesort_cmp: strcasecmp sorting rules but requires strict equality
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 59 60 61 62 63 64 65 66 67 68 69 70
diff --git a/src/util.c b/src/util.c
index da15a03..8536c95 100644
--- a/src/util.c
+++ b/src/util.c
@@ -279,6 +279,31 @@ int git__strcasecmp(const char *a, const char *b)
return (tolower(*a) - tolower(*b));
}
+int git__strcasesort_cmp(const char *a, const char *b)
+{
+ int cmp = 0;
+
+ const char *orig_a = a;
+ const char *orig_b = b;
+
+ while (*a && *b) {
+ if (*a == *b)
+ ;
+ else if (tolower(*a) == tolower(*b)) {
+ if (!cmp)
+ cmp = (int)(*(const unsigned char *)a) - (int)(*(const unsigned char *)b);
+ } else
+ break;
+
+ ++a, ++b;
+ }
+
+ if (*a || *b)
+ return tolower(*a) - tolower(*b);
+
+ return cmp;
+}
+
int git__strncmp(const char *a, const char *b, size_t sz)
{
while (sz && *a && *b && *a == *b)
diff --git a/src/util.h b/src/util.h
index 1ef9e65..e008839 100644
--- a/src/util.h
+++ b/src/util.h
@@ -194,6 +194,8 @@ extern int git__strcasecmp(const char *a, const char *b);
extern int git__strncmp(const char *a, const char *b, size_t sz);
extern int git__strncasecmp(const char *a, const char *b, size_t sz);
+extern int git__strcasesort_cmp(const char *a, const char *b);
+
#include "thread-utils.h"
typedef struct {
diff --git a/tests-clar/core/string.c b/tests-clar/core/string.c
index bf6ec0a..ec95756 100644
--- a/tests-clar/core/string.c
+++ b/tests-clar/core/string.c
@@ -26,3 +26,16 @@ void test_core_string__1(void)
cl_assert(git__suffixcmp("zaz", "ac") > 0);
}
+/* compare icase sorting with case equality */
+void test_core_string__2(void)
+{
+ cl_assert(git__strcasesort_cmp("", "") == 0);
+ cl_assert(git__strcasesort_cmp("foo", "foo") == 0);
+ cl_assert(git__strcasesort_cmp("foo", "bar") > 0);
+ cl_assert(git__strcasesort_cmp("bar", "foo") < 0);
+ cl_assert(git__strcasesort_cmp("foo", "FOO") > 0);
+ cl_assert(git__strcasesort_cmp("FOO", "foo") < 0);
+ cl_assert(git__strcasesort_cmp("foo", "BAR") > 0);
+ cl_assert(git__strcasesort_cmp("BAR", "foo") < 0);
+ cl_assert(git__strcasesort_cmp("fooBar", "foobar") < 0);
+}