Commit e3b4a47c1ebd55931cb25bf5c2af821df9b0bffa

Edward Thomson 2013-05-31T16:30:09

git__strcasesort_cmp: strcasecmp sorting rules but requires strict equality

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);
+}