Commit e3b4a47c by Edward Thomson Committed by Russell Belfer

git__strcasesort_cmp: strcasecmp sorting rules but requires strict equality

parent dfe8c8df
......@@ -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)
......
......@@ -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 {
......
......@@ -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);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment