strcase: use curl_str(n)equal for case insensitive matches

No point in having two entry points for the same functions.

Also merged the *safe* function treatment into these so that they can
also be used when one or both pointers are NULL.

Closes #9837
This commit is contained in:
Daniel Stenberg 2022-10-31 12:49:46 +01:00
parent 7399fa5b05
commit 3f039dfd6f
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
7 changed files with 36 additions and 47 deletions

View file

@ -83,16 +83,13 @@ char Curl_raw_tolower(char in)
}
/*
* Curl_strcasecompare() is for doing "raw" case insensitive strings. This is
* meant to be locale independent and only compare strings we know are safe
* for this. See
* https://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for some
* further explanation to why this function is necessary.
*
* @unittest: 1301
* curl_strequal() is for doing "raw" case insensitive strings. This is meant
* to be locale independent and only compare strings we know are safe for
* this. See https://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for
* further explanations as to why this function is necessary.
*/
int Curl_strcasecompare(const char *first, const char *second)
static int casecompare(const char *first, const char *second)
{
while(*first && *second) {
if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
@ -108,25 +105,22 @@ int Curl_strcasecompare(const char *first, const char *second)
return !*first == !*second;
}
int Curl_safe_strcasecompare(const char *first, const char *second)
/* --- public function --- */
int curl_strequal(const char *first, const char *second)
{
if(first && second)
/* both pointers point to something then compare them */
return Curl_strcasecompare(first, second);
return casecompare(first, second);
/* if both pointers are NULL then treat them as equal */
return (NULL == first && NULL == second);
}
/*
* @unittest: 1301
*/
int Curl_strncasecompare(const char *first, const char *second, size_t max)
static int ncasecompare(const char *first, const char *second, size_t max)
{
while(*first && *second && max) {
if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) {
break;
}
if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
return 0;
max--;
first++;
second++;
@ -137,6 +131,16 @@ int Curl_strncasecompare(const char *first, const char *second, size_t max)
return Curl_raw_toupper(*first) == Curl_raw_toupper(*second);
}
/* --- public function --- */
int curl_strnequal(const char *first, const char *second, size_t max)
{
if(first && second)
/* both pointers point to something then compare them */
return ncasecompare(first, second, max);
/* if both pointers are NULL then treat them as equal if max is non-zero */
return (NULL == first && NULL == second && max);
}
/* Copy an upper case version of the string from src to dest. The
* strings may overlap. No more than n characters of the string are copied
* (including any NUL) and the destination string will NOT be
@ -198,14 +202,3 @@ int Curl_timestrcmp(const char *a, const char *b)
return a || b;
return match;
}
/* --- public functions --- */
int curl_strequal(const char *first, const char *second)
{
return Curl_strcasecompare(first, second);
}
int curl_strnequal(const char *first, const char *second, size_t max)
{
return Curl_strncasecompare(first, second, max);
}