From 378a333ee21b086d8f2ec6a3197b471fe088016d Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sun, 30 Apr 2017 12:57:49 +0200 Subject: [PATCH] refactor cJSONUtils_GetPointer --- cJSON_Utils.c | 22 ++++++++++++---------- cJSON_Utils.h | 2 +- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/cJSON_Utils.c b/cJSON_Utils.c index 9998dcd..9c0c33e 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -245,12 +245,14 @@ static cJSON_bool decode_array_index_from_pointer(const unsigned char * const po return 1; } -CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON *object, const char *pointer) +CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *pointer) { + cJSON *current_element = object; /* follow path of the pointer */ - while ((*pointer++ == '/') && object) + while ((pointer[0] == '/') && (current_element != NULL)) { - if (cJSON_IsArray(object)) + pointer++; + if (cJSON_IsArray(current_element)) { size_t index = 0; if (!decode_array_index_from_pointer((const unsigned char*)pointer, &index)) @@ -258,18 +260,18 @@ CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON *object, const char *pointer) return NULL; } - object = get_array_item(object, index); + current_element = get_array_item(current_element, index); } - else if (cJSON_IsObject(object)) + else if (cJSON_IsObject(current_element)) { - object = object->child; + current_element = current_element->child; /* GetObjectItem. */ - while (object && cJSONUtils_Pstrcasecmp((unsigned char*)object->string, (const unsigned char*)pointer)) + while ((current_element != NULL) && cJSONUtils_Pstrcasecmp((unsigned char*)current_element->string, (const unsigned char*)pointer)) { - object = object->next; + current_element = current_element->next; } /* skip to the next path token or end of string */ - while (*pointer && (*pointer != '/')) + while ((pointer[0] != '\0') && (pointer[0] != '/')) { pointer++; } @@ -280,7 +282,7 @@ CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON *object, const char *pointer) } } - return object; + return current_element; } /* JSON Patch implementation. */ diff --git a/cJSON_Utils.h b/cJSON_Utils.h index 6eab69a..8952645 100644 --- a/cJSON_Utils.h +++ b/cJSON_Utils.h @@ -23,7 +23,7 @@ #include "cJSON.h" /* Implement RFC6901 (https://tools.ietf.org/html/rfc6901) JSON Pointer spec. */ -CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON *object, const char *pointer); +CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *pointer); /* Implement RFC6902 (https://tools.ietf.org/html/rfc6902) JSON Patch spec. */ CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatches(cJSON *from, cJSON *to);