simplify and rename cJSON_strcasecmp

Two NULL strings should not be considered equal for the purpose of
cJSON.
This commit is contained in:
Max Bruckner 2017-04-05 19:30:06 +02:00
parent 31400affab
commit e9803341d5

24
cJSON.c
View File

@ -70,23 +70,17 @@ CJSON_PUBLIC(const char*) cJSON_Version(void)
return version;
}
/* case insensitive strcmp */
static int cJSON_strcasecmp(const unsigned char *string1, const unsigned char *string2)
/* Case insensitive string comparison, doesn't consider two NULL pointers equal though */
static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2)
{
if (string1 == NULL)
if ((string1 == NULL) || (string2 == NULL))
{
if (string2 == NULL)
{
/* both NULL */
return 0;
}
return 1;
}
if (string2 == NULL)
if (string1 == string2)
{
return 1;
return 0;
}
for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++)
@ -97,7 +91,7 @@ static int cJSON_strcasecmp(const unsigned char *string1, const unsigned char *s
}
}
return tolower(string1[0]) - tolower(string2[0]);
return tolower(*string1) - tolower(*string2);
}
typedef struct internal_hooks
@ -1686,7 +1680,7 @@ CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int item)
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON *object, const char *string)
{
cJSON *c = object ? object->child : NULL;
while (c && cJSON_strcasecmp((unsigned char*)c->string, (const unsigned char*)string))
while (c && !case_insensitive_strcmp((unsigned char*)c->string, (const unsigned char*)string))
{
c = c->next;
}
@ -1860,7 +1854,7 @@ CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *stri
{
size_t i = 0;
cJSON *c = object->child;
while (c && cJSON_strcasecmp((unsigned char*)c->string, (const unsigned char*)string))
while (c && !case_insensitive_strcmp((unsigned char*)c->string, (const unsigned char*)string))
{
i++;
c = c->next;
@ -1948,7 +1942,7 @@ CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object, const char *string,
{
size_t i = 0;
cJSON *c = object->child;
while(c && cJSON_strcasecmp((unsigned char*)c->string, (const unsigned char*)string))
while(c && !case_insensitive_strcmp((unsigned char*)c->string, (const unsigned char*)string))
{
i++;
c = c->next;