Make string comparison case sensitive

This fixes an incompatibility with the JSON standard.
This commit is contained in:
Max Bruckner
2016-11-13 18:19:39 +07:00
parent a66da1e560
commit 9ef2ad2f54
2 changed files with 10 additions and 53 deletions

28
cJSON.c
View File

@@ -39,28 +39,6 @@ const char *cJSON_GetErrorPtr(void)
return global_ep; return global_ep;
} }
/* case insensitive strcmp */
static int cJSON_strcasecmp(const char *s1, const char *s2)
{
if (!s1)
{
return (s1 == s2) ? 0 : 1; /* both NULL? */
}
if (!s2)
{
return 1;
}
for(; tolower(*(const unsigned char *)s1) == tolower(*(const unsigned char *)s2); ++s1, ++s2)
{
if (*s1 == '\0')
{
return 0;
}
}
return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2);
}
static cJSON_Hooks global_hooks = { malloc, free }; static cJSON_Hooks global_hooks = { malloc, free };
static char* cJSON_strdup(const char* str, const cJSON_Hooks * const hooks) static char* cJSON_strdup(const char* str, const cJSON_Hooks * const hooks)
@@ -1612,7 +1590,7 @@ cJSON *cJSON_GetArrayItem(const cJSON *array, size_t item)
cJSON *cJSON_GetObjectItem(const cJSON *object, const char *string) cJSON *cJSON_GetObjectItem(const cJSON *object, const char *string)
{ {
cJSON *c = object ? object->child : NULL; cJSON *c = object ? object->child : NULL;
while (c && cJSON_strcasecmp(c->string, string)) while (c && strcmp(c->string, string))
{ {
c = c->next; c = c->next;
} }
@@ -1760,7 +1738,7 @@ cJSON *cJSON_DetachItemFromObject(cJSON *object, const char *string)
{ {
size_t i = 0; size_t i = 0;
cJSON *c = object->child; cJSON *c = object->child;
while (c && cJSON_strcasecmp(c->string,string)) while (c && strcmp(c->string,string))
{ {
i++; i++;
c = c->next; c = c->next;
@@ -1847,7 +1825,7 @@ void internal_cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON
{ {
size_t i = 0; size_t i = 0;
cJSON *c = object->child; cJSON *c = object->child;
while(c && cJSON_strcasecmp(c->string, string)) while(c && strcmp(c->string, string))
{ {
i++; i++;
c = c->next; c = c->next;

View File

@@ -19,29 +19,8 @@ static char* cJSONUtils_strdup(const char* str)
return copy; return copy;
} }
static int cJSONUtils_strcasecmp(const char *s1, const char *s2)
{
if (!s1)
{
return (s1 == s2) ? 0 : 1; /* both NULL? */
}
if (!s2)
{
return 1;
}
for(; tolower(*s1) == tolower(*s2); ++s1, ++s2)
{
if(*s1 == 0)
{
return 0;
}
}
return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2);
}
/* JSON Pointer implementation: */ /* JSON Pointer implementation: */
static int cJSONUtils_Pstrcasecmp(const char *a, const char *e) static int cJSONUtils_Pstrcmp(const char *a, const char *e)
{ {
if (!a || !e) if (!a || !e)
{ {
@@ -62,7 +41,7 @@ static int cJSONUtils_Pstrcasecmp(const char *a, const char *e)
e++; e++;
} }
} }
else if (tolower(*a) != tolower(*e)) else if (*a != *e)
{ {
return 1; return 1;
} }
@@ -185,7 +164,7 @@ cJSON *cJSONUtils_GetPointer(cJSON *object, const char *pointer)
{ {
object = object->child; object = object->child;
/* GetObjectItem. */ /* GetObjectItem. */
while (object && cJSONUtils_Pstrcasecmp(object->string, pointer)) while (object && cJSONUtils_Pstrcmp(object->string, pointer))
{ {
object = object->next; object = object->next;
} }
@@ -292,7 +271,7 @@ static int cJSONUtils_Compare(cJSON *a, cJSON *b)
{ {
int err = 0; int err = 0;
/* compare object keys */ /* compare object keys */
if (cJSONUtils_strcasecmp(a->string, b->string)) if (strcmp(a->string, b->string))
{ {
/* missing member */ /* missing member */
return -6; return -6;
@@ -585,7 +564,7 @@ static void cJSONUtils_CompareToPatch(cJSON *patches, const char *path, cJSON *f
/* for all object values in the object with more of them */ /* for all object values in the object with more of them */
while (a || b) while (a || b)
{ {
int diff = (!a) ? 1 : ((!b) ? -1 : cJSONUtils_strcasecmp(a->string, b->string)); int diff = (!a) ? 1 : ((!b) ? -1 : strcmp(a->string, b->string));
if (!diff) if (!diff)
{ {
/* both object keys are the same */ /* both object keys are the same */
@@ -639,7 +618,7 @@ static cJSON *cJSONUtils_SortList(cJSON *list)
return list; return list;
} }
while (ptr && ptr->next && (cJSONUtils_strcasecmp(ptr->string, ptr->next->string) < 0)) while (ptr && ptr->next && (strcmp(ptr->string, ptr->next->string) < 0))
{ {
/* Test for list sorted. */ /* Test for list sorted. */
ptr = ptr->next; ptr = ptr->next;
@@ -676,7 +655,7 @@ static cJSON *cJSONUtils_SortList(cJSON *list)
while (first && second) /* Merge the sub-lists */ while (first && second) /* Merge the sub-lists */
{ {
if (cJSONUtils_strcasecmp(first->string, second->string) < 0) if (strcmp(first->string, second->string) < 0)
{ {
if (!list) if (!list)
{ {