Unverified Commit 18e836cb by Edward Thomson Committed by GitHub

Merge pull request #5018 from romkatv/strings

Optimize string comparisons
parents 9aa049d4 30a56ba6
......@@ -204,13 +204,6 @@ int git__strntol32(int32_t *result, const char *nptr, size_t nptr_len, const cha
return error;
}
int git__strcmp(const char *a, const char *b)
{
while (*a && *b && *a == *b)
++a, ++b;
return (int)(*(const unsigned char *)a) - (int)(*(const unsigned char *)b);
}
int git__strcasecmp(const char *a, const char *b)
{
while (*a && *b && git__tolower(*a) == git__tolower(*b))
......@@ -240,15 +233,6 @@ int git__strcasesort_cmp(const char *a, const char *b)
return cmp;
}
int git__strncmp(const char *a, const char *b, size_t sz)
{
while (sz && *a && *b && *a == *b)
--sz, ++a, ++b;
if (!sz)
return 0;
return (int)(*(const unsigned char *)a) - (int)(*(const unsigned char *)b);
}
int git__strncasecmp(const char *a, const char *b, size_t sz)
{
int al, bl;
......@@ -301,7 +285,18 @@ GIT_INLINE(int) prefixcmp(const char *str, size_t str_n, const char *prefix, boo
int git__prefixcmp(const char *str, const char *prefix)
{
return prefixcmp(str, SIZE_MAX, prefix, false);
unsigned char s, p;
while (1) {
p = *prefix++;
s = *str++;
if (!p)
return 0;
if (s != p)
return s - p;
}
}
int git__prefixncmp(const char *str, size_t str_n, const char *prefix)
......
......@@ -150,12 +150,13 @@ extern int git__bsearch_r(
void *payload,
size_t *position);
#define git__strcmp strcmp
#define git__strncmp strncmp
extern int git__strcmp_cb(const void *a, const void *b);
extern int git__strcasecmp_cb(const void *a, const void *b);
extern int git__strcmp(const char *a, const char *b);
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);
......
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