From 649af9c2c3a3537e797ac037df210772485a0b23 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 3 Feb 2018 16:42:43 +0100 Subject: [PATCH] cJSON_Compare: Extract compare with internal_configuration --- cJSON.c | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/cJSON.c b/cJSON.c index fafb6a7..ffe7909 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1822,7 +1822,7 @@ CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index) return get_array_item(array, (size_t)index); } -static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive) +static cJSON *get_object_item(const cJSON * const object, const char * const name, const internal_configuration * const configuration) { cJSON *current_element = NULL; @@ -1832,7 +1832,7 @@ static cJSON *get_object_item(const cJSON * const object, const char * const nam } current_element = object->child; - if (case_sensitive) + if (configuration->case_sensitive) { while ((current_element != NULL) && (strcmp(name, current_element->string) != 0)) { @@ -1852,12 +1852,16 @@ static cJSON *get_object_item(const cJSON * const object, const char * const nam CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string) { - return get_object_item(object, string, false); + internal_configuration configuration = default_configuration; + configuration.case_sensitive = false; + return get_object_item(object, string, &configuration); } CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string) { - return get_object_item(object, string, true); + internal_configuration configuration = default_configuration; + configuration.case_sensitive = true; + return get_object_item(object, string, &configuration); } CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string) @@ -2260,7 +2264,7 @@ CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newi cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem); } -static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive) +static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, const internal_configuration * const configuration) { if ((replacement == NULL) || (string == NULL)) { @@ -2275,19 +2279,23 @@ static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSO replacement->string = (char*)custom_strdup((const unsigned char*)string, &global_configuration); replacement->type &= ~cJSON_StringIsConst; - cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement); + cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, configuration), replacement); return true; } CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem) { - replace_item_in_object(object, string, newitem, false); + internal_configuration configuration = global_configuration; + configuration.case_sensitive = false; + replace_item_in_object(object, string, newitem, &configuration); } CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem) { - replace_item_in_object(object, string, newitem, true); + internal_configuration configuration = global_configuration; + configuration.case_sensitive = true; + replace_item_in_object(object, string, newitem, &configuration); } /* Create basic types: */ @@ -2831,7 +2839,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item) return (item->type & 0xFF) == cJSON_Raw; } -CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive) +static cJSON_bool compare(const cJSON * const a, const cJSON * const b, const internal_configuration * const configuration) { if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)) || cJSON_IsInvalid(a)) { @@ -2896,7 +2904,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * cons for (; (a_element != NULL) && (b_element != NULL);) { - if (!cJSON_Compare(a_element, b_element, case_sensitive)) + if (!compare(a_element, b_element, configuration)) { return false; } @@ -2928,13 +2936,13 @@ CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * cons cJSON_ArrayForEach(a_element, a) { /* TODO This has O(n^2) runtime, which is horrible! */ - b_element = get_object_item(b, a_element->string, case_sensitive); + b_element = get_object_item(b, a_element->string, configuration); if (b_element == NULL) { return false; } - if (!cJSON_Compare(a_element, b_element, case_sensitive)) + if (!compare(a_element, b_element, configuration)) { return false; } @@ -2948,6 +2956,13 @@ CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * cons } } +CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive) +{ + internal_configuration configuration = global_configuration; + configuration.case_sensitive = case_sensitive; + return compare(a, b, &configuration); +} + CJSON_PUBLIC(void *) cJSON_malloc(size_t size) { return global_configuration.allocate(size);