From 5713edb71081dcc02e772468c3b24e68dfb724fc Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Fri, 14 Oct 2016 00:18:15 +0700 Subject: [PATCH] reformatting: cJSON_Utils_GetPointer NOTE: This can change the assembly slightly, in my case it reordered two instructions. This is due to the change from: which = (10 * which) + *pointer++ - '0'; to which = (10 * which) + (*pointer++ - '0'); This means that after the change, the subtraction runs before the addition instead of after that. That shouldn't change the behavior though. --- cJSON_Utils.c | 57 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/cJSON_Utils.c b/cJSON_Utils.c index ac5df2c..afbb593 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -146,24 +146,47 @@ char *cJSONUtils_FindPointerFromObjectTo(cJSON *object, cJSON *target) return 0; } -cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer) +cJSON *cJSONUtils_GetPointer(cJSON *object, const char *pointer) { - while (*pointer++=='/' && object) - { - if (object->type==cJSON_Array) - { - int which=0; while (*pointer>='0' && *pointer<='9') which=(10*which) + *pointer++ - '0'; - if (*pointer && *pointer!='/') return 0; - object=cJSON_GetArrayItem(object,which); - } - else if (object->type==cJSON_Object) - { - object=object->child; while (object && cJSONUtils_Pstrcasecmp(object->string,pointer)) object=object->next; /* GetObjectItem. */ - while (*pointer && *pointer!='/') pointer++; - } - else return 0; - } - return object; + /* follow path of the pointer */ + while ((*pointer++ == '/') && object) + { + if (object->type == cJSON_Array) + { + int which = 0; + /* parse array index */ + while ((*pointer >= '0') && (*pointer <= '9')) + { + which = (10 * which) + (*pointer++ - '0'); + } + if (*pointer && (*pointer != '/')) + { + /* not end of string or new path token */ + return 0; + } + object = cJSON_GetArrayItem(object, which); + } + else if (object->type == cJSON_Object) + { + object = object->child; + /* GetObjectItem. */ + while (object && cJSONUtils_Pstrcasecmp(object->string, pointer)) + { + object = object->next; + } + /* skip to the next path token or end of string */ + while (*pointer && (*pointer != '/')) + { + pointer++; + } + } + else + { + return 0; + } + } + + return object; } /* JSON Patch implementation. */