Просмотр исходного кода

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.
Max Bruckner 8 лет назад
Родитель
Сommit
5713edb710
1 измененных файлов с 40 добавлено и 17 удалено
  1. 40 17
      cJSON_Utils.c

+ 40 - 17
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. */