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.
This commit is contained in:
Max Bruckner 2016-10-14 00:18:15 +07:00
parent 158ddceab3
commit 5713edb710

View File

@ -146,24 +146,47 @@ char *cJSONUtils_FindPointerFromObjectTo(cJSON *object, cJSON *target)
return 0; return 0;
} }
cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer) cJSON *cJSONUtils_GetPointer(cJSON *object, const char *pointer)
{ {
while (*pointer++=='/' && object) /* follow path of the pointer */
{ while ((*pointer++ == '/') && object)
if (object->type==cJSON_Array) {
{ if (object->type == cJSON_Array)
int which=0; while (*pointer>='0' && *pointer<='9') which=(10*which) + *pointer++ - '0'; {
if (*pointer && *pointer!='/') return 0; int which = 0;
object=cJSON_GetArrayItem(object,which); /* parse array index */
} while ((*pointer >= '0') && (*pointer <= '9'))
else if (object->type==cJSON_Object) {
{ which = (10 * which) + (*pointer++ - '0');
object=object->child; while (object && cJSONUtils_Pstrcasecmp(object->string,pointer)) object=object->next; /* GetObjectItem. */ }
while (*pointer && *pointer!='/') pointer++; if (*pointer && (*pointer != '/'))
} {
else return 0; /* not end of string or new path token */
} return 0;
return object; }
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. */ /* JSON Patch implementation. */