From e9803341d5ce2c79548cc4791551115e38503223 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Wed, 5 Apr 2017 19:30:06 +0200 Subject: [PATCH] simplify and rename cJSON_strcasecmp Two NULL strings should not be considered equal for the purpose of cJSON. --- cJSON.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/cJSON.c b/cJSON.c index edd9310..c73ff03 100644 --- a/cJSON.c +++ b/cJSON.c @@ -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;