]> git.feebdaed.xyz Git - 0xmirror/cJSON.git/commitdiff
optimize the way to find tail node
authorAlanscut <wp_scut@163.com>
Wed, 2 Sep 2020 12:23:52 +0000 (20:23 +0800)
committerAlanscut <wp_scut@163.com>
Wed, 2 Sep 2020 12:23:52 +0000 (20:23 +0800)
cJSON.c
cJSON_Utils.c

diff --git a/cJSON.c b/cJSON.c
index 7518389b5392d391ee20e01e61819ddaa7d31107..0acf487d0bea7e6041951a04a8b761286a865a25 100644 (file)
--- a/cJSON.c
+++ b/cJSON.c
@@ -1975,15 +1975,6 @@ static cJSON_bool add_item_to_array(cJSON *array, cJSON *item)
             suffix_object(child->prev, item);
             array->child->prev = item;
         }
-        else
-        {
-            while (child->next)
-            {
-                child = child->next;
-            }
-            suffix_object(child, item);
-            array->child->prev = item;
-        }
     }
 
     return true;
@@ -2571,6 +2562,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count)
         }
         p = n;
     }
+    a->child->prev = n;
 
     return a;
 }
@@ -2607,6 +2599,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
         }
         p = n;
     }
+    a->child->prev = n;
 
     return a;
 }
@@ -2643,6 +2636,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count)
         }
         p = n;
     }
+    a->child->prev = n;
 
     return a;
 }
@@ -2679,6 +2673,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int co
         }
         p = n;
     }
+    a->child->prev = n;
 
     return a;
 }
@@ -2751,6 +2746,10 @@ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
         }
         child = child->next;
     }
+    if (newitem && newitem->child)
+    {
+        newitem->child->prev = newchild;
+    }
 
     return newitem;
 
index c750f7fcbb0e0e037d19deeb4444063115c6e8a8..f4ad32a53044ab083012f08e3fbba2d6b78f6c16 100644 (file)
@@ -403,7 +403,7 @@ static cJSON *detach_item_from_array(cJSON *array, size_t which)
         /* item doesn't exist */
         return NULL;
     }
-    if (c->prev)
+    if (c != array->child)
     {
         /* not the first element */
         c->prev->next = c->next;
@@ -412,10 +412,14 @@ static cJSON *detach_item_from_array(cJSON *array, size_t which)
     {
         c->next->prev = c->prev;
     }
-    if (c==array->child)
+    if (c == array->child)
     {
         array->child = c->next;
     }
+    else if (c->next == NULL)
+    {
+        array->child->prev = c->prev;
+    }
     /* make sure the detached item doesn't point anywhere anymore */
     c->prev = c->next = NULL;