mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-29 22:25:04 +00:00
git__strcasesort_cmp: strcasecmp sorting rules but requires strict equality
This commit is contained in:
parent
dfe8c8df37
commit
e3b4a47c1e
25
src/util.c
25
src/util.c
@ -279,6 +279,31 @@ int git__strcasecmp(const char *a, const char *b)
|
|||||||
return (tolower(*a) - tolower(*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)
|
int git__strncmp(const char *a, const char *b, size_t sz)
|
||||||
{
|
{
|
||||||
while (sz && *a && *b && *a == *b)
|
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__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__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"
|
#include "thread-utils.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -26,3 +26,16 @@ void test_core_string__1(void)
|
|||||||
cl_assert(git__suffixcmp("zaz", "ac") > 0);
|
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);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user