summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2019-04-04 10:55:38 -0700
committerGitHub <noreply@github.com>2019-04-04 10:55:38 -0700
commit18e836cbf3f01d1aaa839315efb2c0cf0e6b2204 (patch)
treea9e018eddedda7bf3f6565df0bbf00e600c028ef
parent9aa049d4c1578cfd73ce66801fe2575632296060 (diff)
parent30a56ba644e0f0d34c71658c9a53328a1ccc3538 (diff)
downloadlibgit2-18e836cbf3f01d1aaa839315efb2c0cf0e6b2204.tar.gz
Merge pull request #5018 from romkatv/strings
Optimize string comparisons
-rw-r--r--src/util.c29
-rw-r--r--src/util.h5
2 files changed, 15 insertions, 19 deletions
diff --git a/src/util.c b/src/util.c
index a49269b98..508dce504 100644
--- a/src/util.c
+++ b/src/util.c
@@ -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)
diff --git a/src/util.h b/src/util.h
index 4314295f1..d4fe6eee7 100644
--- a/src/util.h
+++ b/src/util.h
@@ -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);